微信插件

在开发微信插件支持,请先确认对微信进行正确的配置,微信配置可以查看:http://www.jpress.io/article/65

微信插件开发步骤

  • 1、编写任意类,实现接口 WechatAddon
  • 2、给该类添加 @WechatAddonConfig 注解。

以下是 完整的 hello world 例子,当用户在微信客户端给公众号输入 hello 的时候,服务器给微信返回 world 字符串:

@WechatAddonConfig(
        id = "ip.press.helloaddon", //这个插件的ID,配置整个 JPress 系统全局唯一
        title = "Hello World", //这个插件的标题,用于在后台显示
        description = "这是一个 Hello World 微信插件,方便开发参考。用户输入 hello,返回 world", //这个插件的描述
        author = "海哥" //这个插件的作者
)
public class HelloWechatAddon implements WechatAddon {

    @Override
    public boolean onMatchingMessage(InMsg inMsg, MsgController msgController) {
        
        //当用户给公众号发送的不是文本消息的时候
        //返回 false 不由本插件处理
        if (!(inMsg instanceof InTextMsg)) {
            return false;
        }

        InTextMsg inTextMsg = (InTextMsg) inMsg;
        String content = inTextMsg.getContent();
        
        //当用户输入的内容不是 hello 的时候
        //返回false,不由本插件处理
        return content != null && content.equalsIgnoreCase("hello");
    }


    @Override
    public boolean onRenderMessage(InMsg inMsg, MsgController msgController) {
    
        //创建一个新的文本消息
        //通过 msgController 进行渲染返回给用户
        OutTextMsg outTextMsg = new OutTextMsg(inMsg);
        outTextMsg.setContent("world");
        msgController.render(outTextMsg);
        
        //返回 true,表示本插件已经成功处理该消息
        //若返回false,表示本插件处理消息失败,将会交给系统或者其他插件去处理
        return true;
    }
}

备注:

  • onMatchingMessage :用来匹配是否由本插件处理该消息,返回 true 由本插件处理,false 本插件忽略此消息。
  • onRenderMessage :用来返回给微信客户端一个消息

加入VIP


如果你想系统学习JPress涉及到的知识点,或者希望有人解答你在深度使用JPress时遇到问题,加入VIP是个很不错的选择

立马 了解一下