Skip to content

Micro App Add Messaging for Micro App Sample Code

This guide is to demonstrate how to construct a WeChatMessage and push it to end users.

Sample 1: push a text card to the sender user

public class CustomMessageService extends DefaultMessageService {
    @Override
    public void consumeMessage(WeChatMessage message) throws MicroAppException {
        // In WeChatMessage object message,
        // fromUser should be message sender account, e.g. P000001
        // and toUser should be wechat corp account, e.g. ww2d8dfb3f1e4a4e60
        String fromUser = message.getFromUserName();
        String toUser = message.getToUserName();
        WeChatMessage msg = new WeChatMessage();
        // Then We need to construct a WeChatMessage with consructing methods
        // e.g. build a text card message and push it back to the fromUser
        msg.withTextCardMessage(fromUser, toUser, "Title", "description", "www.bing.com", "Click Me");
        // push the constructed message
        PushMessage pushMsg = new PushMessage(msg);
        super.sendMessage(pushMsg);
    }
}

Sample 2: push a pong text response to sender user

public class CustomMessageService extends DefaultMessageService {

    @Override
    public void consumeMessage(WeChatMessage message) throws MicroAppException {
        // In WeChatMessage object message,
        // fromUser should be message sender account, e.g. P000001
        // and toUser should be wechat corp account, e.g. ww2d8dfb3f1e4a4e60
        String fromUser = message.getFromUserName();
        String toUser = message.getToUserName();
        // build a text message with text pong and push it back to fromUser
        WeChatMessage msg = new WeChatMessage().withTextMessage(fromUser, toUser, "pong");

        // push the message
        super.sendMessage(new PushMessage(msg));
    }
}

Sample 3: push a news to some users

public class CustomMessageService extends DefaultMessageService {

    @Override
    public void consumeMessage(WeChatMessage message) throws MicroAppException {
        // In WeChatMessage object message,
        // fromUser should be message sender account, e.g. P000001
        // and toUser should be wechat corp account, e.g. ww2d8dfb3f1e4a4e60
        String fromUser = message.getFromUserName();
        String toUser = message.getToUserName();

        // build a "news" type message and push it to 3 different users
        WeChatMessage msg = new WeChatMessage().withNewsMessage("p000001|p000002|p000003", toUser)
                .appendNews("news1", "description for news1", "Pic url1", "www.sap.com")
                .appendNews("news2", "description for news2", "Pic url2", "www.sap.com");
        // push the message
        super.sendMessage(new PushMessage(msg));
    }
}

Sample 4: Other types of message push

public class CustomMessageService extends DefaultMessageService {

    @Override
    public void consumeMessage(WeChatMessage message) throws MicroAppException {
        // In WeChatMessage object message,
        // fromUser should be message sender account, e.g. P000001
        // and toUser should be wechat corp account, e.g. ww2d8dfb3f1e4a4e60
        String fromUser = message.getFromUserName();
        String toUser = message.getToUserName();
        List<WeChatMessage> msgList = new ArrayList<WeChatMessage>();

        // markdown message back to the sender
        WeChatMessage markdownMsg = new WeChatMessage().withMarkdownMessage(fromUser, toUser,
                "**Note:** please click´╝Ü[modify resource](https://www.sap.com)");
        msgList.add(markdownMsg);

        // image message back to the sender
        WeChatMessage imageMsg = new WeChatMessage().withImageMessage(fromUser, toUser,
                "3PUVUKUniL4TN0DN8sER_O3rKhDFFHiHFERB8p3yUmY67i9YnpqGzoFuiimiie8ER");
        msgList.add(imageMsg);

        // voice message back to the sender
        WeChatMessage voiceMsg = new WeChatMessage().withVoiceMessage(fromUser, toUser,
                "3VEkzWqGHv3o6XGOrQKiDhV6fLQGuAJFJpZfo0P05pDg");
        msgList.add(voiceMsg);

        // video message back to the sender
        WeChatMessage videoMsg = new WeChatMessage().withVideoMessage(fromUser, toUser,
                "3CRkurqknbzRGAuUXAhUxDT23tuozo8IXECkjupQ53TcHwM6suAL23sy9EzEBhjqg", "video title",
                "video description");
        msgList.add(videoMsg);

        // file message back to the sender
        WeChatMessage fileMsg = new WeChatMessage().withFileMessage(fromUser, toUser,
                "3giPcoDI0s_yrikTdPp2SGxByRqnFXmCb3mKtGcOZZzw");
        msgList.add(fileMsg);

        // mpnews message back to the sender
        WeChatMessage mpnewsMsg = new WeChatMessage().withMPNewsMessage(fromUser, toUser)
                .appendMPNews("title1", "3PUVUKUniL4TN0DN8sER_O3rKhDFFHiHFERB8p3yUmY67i9YnpqGzoFuiimiie8ER", "perry1",
                        "http://www.bing.com", "content1", "digest1")
                .appendMPNews("title2", "3PUVUKUniL4TN0DN8sER_O3rKhDFFHiHFERB8p3yUmY67i9YnpqGzoFuiimiie8ER", "perry2",
                        "http://www.bing.com", "content2", "digest2");
        msgList.add(mpnewsMsg);

        // push all messages
        for (WeChatMessage msg : msgList) {
            super.sendMessage(new PushMessage(msg));
        }
    }
}

Sample 5: Update back-end data, a notification message can be triggered in WeChat

@Override public void afterUpdate(EntityValue entityValue) {
    example.proxy.Customer entity = (example.proxy.Customer)entityValue;
    allowUnused(entity);
    afterSave(entity);
    processUpdate(entity); // to trigger notification
}



private void processUpdate(example.proxy.Customer newCustomer) {
    example.MainServlet mainServlet = (example.MainServlet) DataServlet.getCurrentServlet();
    String currentUser = mainServlet.currentUser();
    currentUser = currentUser.indexOf("/") > 0? currentUser.substring(currentUser.lastIndexOf("/")+1) : currentUser;
    String pushContent = String.format("Customer Id << %d >>: Name has changed to %s",
              newCustomer.getCustomerID(),
              newCustomer.getName());

    PushMessage pushMessage = new PushMessage(new WeChatMessage().withTextMessage(currentUser, "bas_test", pushContent));
    try {
          WeChatNotificationHandler.getInstance().sendMessage(pushMessage);
    } catch (Exception ex) {
          log.debug("error in sending push notification to user {} : ", ex);
    }
}

Last update: August 12, 2020