本文目录
显示
1.
1. 配置微信公众号
2.
2. 编写服务端
3.
3. 处理业务逻辑
3.1.
推荐阅读
顾名思义,我们就是来做一个订阅号机器人,大致是这样一个过程:公众号接收用户消息 -> 微信平台发送消息给我们的服务器 -> 我们的服务器处理消息 -> 返回处理结果给微信平台 -> 微信平台发送内容给用户。
基于这样一个大前提就有了下面的步骤:
1、填写服务器配置,可以接收微信平台发送的内容
2、开发服务端,并验证服务器地址的有效性
3、处理具体的业务逻辑
1. 配置微信公众号
首先肯定需要有一个订阅号,然后在订阅号后台点击 开发者->基本配置进入如下页面,点击确定
然后进入配置页面,我们一一对配置进行讲解
开发者id,开发者调用的唯一标示,调用接口的时候需要传递。开发者密码,这个很重要一定要保存在自己的服务器上面,用于验证安全性。服务地址,这个就是我们用来接收微信平台转发的用户消息的服务的地址令牌,用户接收信息时候做验证是否请求来自微信平台用于加密消息,防止被截获,如果 6 设置为明文模式不需要这个配置。是否加密传输消息
我们本期只做接收图片消息,验证完成以后回复消息,所以只需要配置 3、4。
是我们具体的服务器地址,path是 weixin/receive 这个下文中具体代码部分会详细讲解token 随便生成一个 uuid 就可以随机生成,后面如果调用 api 会用到。这时候你点击提交会提示验证失败,是因为你还没有部署 api,配置到这里我们就开始编写代码。
2. 编写服务端
服务器端使用现有的轮子非常简单,因为是 spring-boot 项目,直接引入一个现成的微信 starter,一定要添加 repository ,这个是依托 github 自带的仓库。
developer-weapons-repositoryhttps://raw.githubusercontent.com/developer-weapons/repository/mastercom.github.developer.weaponswechat-spring-boot-starter1.2.6然写两个接口,一个 get 用于第一次绑定微信后台验证用,一个 post 用于以后接收消息 /weixin/receive
把之前准备好的 token 配置到 application.properties 然后注入到 controller 里面,大致的验证代码如下,如果验证签名成功就返回 echostr,算是通信的标示,如果验证失败返回 error。
@autowiredprivate wechatofficialservice wechatofficialservice;@value(\${weixin.token}\)private string token;@requestmapping(value = \/weixin/receive\, method = requestmethod.get)public void receive( @requestparam(value = \signature\) string signature, @requestparam(value = \timestamp\) string timestamp, @requestparam(value = \nonce\) string nonce, @requestparam(value = \echostr\) string echostr, httpservletresponse response) throws ioexception { boolean valid = wechatofficialservice.isvalid(signature, timestamp, nonce, token); printwriter writer = response.getwriter(); if (valid) { writer.print(echostr); } else { writer.print(\error\); } writer.flush(); writer.close();}编写到这里就可以找一个服务器部署起来,点击验证喽,这时候点击提交直接成功了,点击启用以后就生效了,生效以后你原来配置的自动回复就会生效,所以这个操作请谨慎。
3. 处理业务逻辑
处理业务逻辑首先是接收消息,下面是接收消息的代码。
@requestmapping(value = \/weixin/receive\, method = requestmethod.post)public void receive( @requestparam(value = \signature\) string signature, @requestparam(value = \timestamp\) string timestamp, @requestparam(value = \nonce\) string nonce, httpservletrequest request, httpservletresponse response) throws ioexception { request.setcharacterencoding(\utf-8\); response.setcharacterencoding(\utf-8\); boolean valid = wechatofficialservice.isvalid(signature, timestamp, nonce, token); printwriter writer = response.getwriter(); if (!valid) { writer.print(\error\); writer.flush(); writer.close(); return; } try { map map = wechatofficialservice.tomap(request.getinputstream()); if (map.get(\msgtype\).equals(\image\)) { string msg = officialautoreplymessage.build() .withcontent(\接收到图片链接为:\ map.get(\picurl\)) .withmsgtype(messagetypeenum.text) .withfromusername(map.get(\tousername\)) .withtousername(map.get(\fromusername\)) .toxml(); writer.print(msg); writer.flush(); writer.close(); return; } } catch (exception e) { log.error(\weixincontroller receive error\, e); } writer.print(\success\); writer.flush(); writer.close();}第一步还是验证消息是否来自微信平台,然后使用 wechatofficialservice.tomap 方法解析出接收消息的内容,当前判断比较简单,直接判断是否是图片消息,然后返回图片的 url 给发送消息的用户。效果图如下
那么接下来就到了最关键的一步,如何鉴黄,这个具体的逻辑可以参考这一篇文章《通过ucloud ai内容审核uai-censor免费搭建鉴黄平台教程》,现在我们直接把相关代码怼上。
按照上面的文章修改代码后结果如下,具体的 publickey 和 privatekey 自己参考下哦。
if (map.get(\msgtype\).equals(\image\)) { string res = checkservice.check(publickey, privatekey, map.get(\picurl\)); officialautoreplymessage officialautoreplymessage = officialautoreplymessage.build() .withmsgtype(messagetypeenum.text) .withfromusername(map.get(\tousername\)) .withtousername(map.get(\fromusername\)); if (stringutils.equals(\forbid\, res)) { officialautoreplymessage.withcontent(\小哥,你的图片有点问题哦\); } else { officialautoreplymessage.withcontent(\骚年,你这图片刚刚的没问题\); } writer.print(officialautoreplymessage.toxml()); writer.flush(); writer.close(); return;}最终效果如下
所以你会搭建自己的鉴黄机器人了吗?获取项目源码>>(测试订阅号“q谈面试”,“q谈面试”订阅号鉴黄能力仅供学习使用,后期会下线,如需体验请尽快。)
来源:码匠笔记
网店二级域名选什么样的好?自适应网站制作注意事项什么免费域名注册平台做电商需要准备什么?做商城网站有什么技巧?网站建设时,最容易犯的错误总结网站SEO优化中用户体验的重要性网站空间购买多少钱?网站空间和服务器有什么区别?腾讯云服务器怎么做快照平台网站建设有哪些 一个网站建设包含哪些内容?