Python创建微信机器人
微信,一个日活10亿的超级app,不仅在国内社交独领风骚,在国外社交也同样占有一席之地,今天我们要将便是如何用Python来生成一个微信机器人,突然想起鲁迅先生曾经说过的一句话:
因为是微信机器人系列的第一篇文章,所以猪哥会特别详细的讲解每一地方,尽量使每一位想学习的同学都能顺顺利利的开始,下面就让我们一起来做些有趣的事吧!
如何监听别人的消息?如何实现放撤回?扫描下方二维码关注微信公众号「裸睡的猪」回复:微信机器人即可获取教程!一、项目介绍1.微信库选择python关于开发微信的库主要有itchat和wxpy,而wxpy底层是调用的itchat,所以如果你只是要使用的话建议使用wxpy库,它比其他的库都要优雅,更面向对象,而且深度整合了图灵机器人和小i机器人;而itchat扩展性更好,如果你想自己开发一个自己的微信库那建议选itchat。
2.实现原理我相信有不少同学使用过微信的网页版,而wxpy(底层使用itchat)库就是模拟登录网页端,然后调用微信的api实现操作的,我们可以查看itchat源码发现。
总之大家记住,目前wxpy和itchat都是模拟网页版微信来操作的。
3.图灵机器人既然可以模拟网页微信了,那又如何做到自动回复呢?这里我们就使用到了图灵机器人,大家可以在他们的官网(http://www.tuling123.com)免费注册账号,然后申请一个免费的机器人,每个人最多免费申请五个机器人。
我会在项目代码中给一个默认的apikey,让大家不用下载就可以尝试,但是还是建议自己去申请,因为这个默认的apikey有调用次数限制,况且这算是一笔免费的财富呢!
4.整体流程为了方便大家理解,猪哥给大家画了一个时序图
二、项目代码先来张项目结构图压压惊:
1.下载项目猪哥已经把项目放在了GitHub上,群里有小伙伴反馈不会使用github,这里我就详细讲一下如何从github上下载项目。
注意:github与git不是同一个东西,github是全球最大的同性恋交友论坛,在这里我们不比颜值与财富,只比谁的项目star多,star越多也就说明你越吸引同性的喜欢与爱慕,甚至连你的同事也会爱上你!而git是项目管理工具,github上的项目就是用git来管理,项目管理工具另一派系是svn。
首先找到你需要下载的项目,然后点击Cloneordownload,然后点击右侧的复制按钮,猪哥这个项目的地址是:https://github.com/pig6/wxrobot
然后打开你的pycharm,选择CSV->Checkoutfromversioncontrol->git,然后粘贴刚才复制的项目链接。
最后pycharm可能会提示你使用新窗口打开还是用当前窗口打开,猪哥一般习惯使用新窗口(NewWindow)打开,这样可以避免多个项目开发时造成混乱。
2.下载wxpy库项目下载下来之后,因为没有安装必须的库wxpy,pycharm可能会有如下提示,这时我们点一下install就可以。
如果没有出现安装库提示的话,我们可以在Setting->Project->ProjectInterpreter里面添加wxpy库。
或者使用以下命令下载wxpy库,如果你是pip3则替换下面的pip。
pipinstall-Uwxpy-i“https://pypi.doubanio.com/simple/”
3.运行项目你可以按右上角的绿色三角形按钮,也可以右键项目然后点击run。
运行之后弹出登录二维码,手机微信扫一扫点击确定登录即可和好友聊天。
三、总结首先感谢大家的耐心阅读,考虑到有很多零基础的同学所以文章有点长。
猪哥来总结下这几天使用wxpy库开发微信机器人的一些感受吧!
猪哥用自己的大号测试了一周,也没有出现被封的迹象,只要不发送大量的相同信息就没问题即使出现被封也只是会限制你微信登录网页版,手机端不影响使用,正常使用大概一两周就自动解封最好用小号测试,一年以内注册的微信号不可以登微信网页版,也就是说不能用测试微信网页版有些功能位阉割了,比如:添加好友,拉人入群等,因为怕微商使用机器人到处作恶所以才阉割目前只开发了一个功能,先让大家入门,后面的功能会慢慢丰富起来目前wxpy基于微信网页登录,很多功能无法实现,后期考虑跟换成iPad登录图灵机器人智商有待提高最后感谢武亚飞同学提供的微信小号wxpy官方文档:https://wxpy.readthedocs.io/zh/latest
获取源码:vx扫描下方二维码,关注vx公号「裸睡的猪」,回复:微信机器人即可获取!
企业微信Robot(群聊机器人)消息推送Java服务
目录背景需求目标企业微信群聊机器人微信官方文档简单介绍一下名词解释具体实现类清单及功能说明MessageSendServiceEnterpriceWechatRobotMessageSendServiceEnterpriseWeChatRobotMessageSendServiceImplEnterpriseRobotMessageDO测试Demo(使用方式)服务Bean构建测试用例背景需求目标本次要做的东西需要满足如下要求
方便快捷地生成对应类型的消息实体(建造者模式)提供一套Service实现,可以让各个模块快速调用并发送消息,并能够提供如下的使用模式构建服务时指定发送机器人,发送时无需关注,仅调用服务消息实体内自带RobotKey,服务根据RobotKey发送到指定机器人OK,先简单介绍一下企业微信群聊机器人
企业微信群聊机器人最近发现企业微信的robot特别好用,可以用较为简便的方式推送消息,甚至可以将机器人加入不同的群聊,灵活推送各类消息。完成一些企业微信服务号完成不了的工作
微信官方文档《如何配置群聊机器人》
简单介绍一下直接在群聊上点击右键即可添加“群聊机器人”点击已添加的机器人可看到webhook地址通过使用说明,可以推送不同种类的消息到群聊内
名词解释企业微信群聊机器人:本次需求的目标调用对象,可以在企业微信群聊中发送指定格式消息,下称RobotRobotKey:企业微信机器人WebHook接口的Key参数。如https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8606ac1f-dc01-423s3-9d74-121343ef539的RobotKey为8606ac1f-dc01-423s3-9d74-121343ef539具体实现类清单及功能说明MessageSendService基类服务接口,定义发送服务使用泛型定义发送的消息实体,继承接口可定义具体定义实体类EnterpriceWechatRobotMessageSendService企业微信Robot消息发送服务继承MessageSendService,定义消息实体类EnterpriseWeChatRobotMessageSendServiceImplEnterpriceWechatRobotMessageSendService的实现类EnterpriseRobotMessageDO消息实体类,用于构建Robot消息ResultDTO返回结果的实体,可自行定义,不做具体赘述MessageSendService基类服务接口,定义发送服务使用泛型定义发送的消息实体,继承接口可定义具体定义实体类使用泛型的目的:定义MessageSend系列服务,可扩展其他的消息发送方式packagedemo.service.wechat.inter;importdemo.common.ResultDTO;/***MessageSendService*企业微信:微信消息推送接口**@authorJohnChen*@since2019/12/12*/publicinterfaceMessageSendService{/***推送消息**@parammsg消息体*@return返回推送结果*/ResultDTOsendMassage(Tmsg);}EnterpriceWechatRobotMessageSendService企业微信Robot消息发送服务继承MessageSendService,定义消息实体类没有定义新的接口方法,沿用MessageSendService内的方法作为唯一实现方法packagedemo.service.wechat.inter;importdemo.model.wechat.EnterpriseRobotMessageDO;/***EnterpriceWechatRobotMessageSendService*企业微信机器人消息发送接口**@authorJohnChen*@since2019/12/13*/publicinterfaceEnterpriceWechatRobotMessageSendServiceextendsMessageSendService{}EnterpriseWeChatRobotMessageSendServiceImplEnterpriceWechatRobotMessageSendService的实现类说明一下其中几个自有类,可以在实际使用中删除SkynetUtils:封装了公司内部日志系统日志记录功能的实现类(记录日志)EnumSkynetLogModule:日志记录相关EnumSkynetCategoryWechatMessageSend:日志记录相关IllegalInputVariableException:自定义Exception,可以在实际使用时变更为ExceptionOkHttp3Utils:OKHttp3封装的http服务类,用于发起http请求,可使用HTTPClient等框架代替。具体实现可参看我的另一篇原创文章《(Java)高性能Http框架:OKHttp3的工具类OkHttp3Utils实现(可使用Http代理)》packagedemo.service.wechat.impl;importcom.alibaba.fastjson.JSONObject;importcom.google.gson.Gson;importdemo.common.ResultDTO;importdemo.common.enumlibrary.skynet.EnumSkynetLogModule;importdemo.common.enumlibrary.skynet.category.EnumSkynetCategoryWechatMessageSend;importdemo.common.myexception.IllegalInputVariableException;importdemo.common.utls.OkHttp3Utils;importdemo.common.utls.SkynetUtils;importdemo.model.wechat.EnterpriseRobotMessageDO;importdemo.service.wechat.inter.EnterpriceWechatRobotMessageSendService;importlombok.extern.slf4j.Slf4j;importorg.apache.commons.lang3.StringUtils;/***EnterpriseWeChatRobotMessageSendServiceImpl*企业微信机器人消息推送接口**@authorJohnChen*@since2019/12/12*/@Slf4jpublicclassEnterpriseWeChatRobotMessageSendServiceImplimplementsEnterpriceWechatRobotMessageSendService{privatefinalstaticStringMODULE=EnumSkynetLogModule.WECHAT_MESSAGE_SEND.getName();privatefinalstaticStringCATEGORY=EnumSkynetCategoryWechatMessageSend.ENTERPRISE_ROBOT.getName();/***企业微信robot推送地址。在构建的时候传进来*/privatefinalStringdefaultPushUrl;/***企业微信robot推送任务名称,无实际逻辑用途,仅用于记录日志*/privatefinalStringjobName;privatefinalGsongson;/***企业微信机器人推送地址*/privatefinalStringwxEnterpriseRobotPushUrl;privatestaticfinalStringERR_CODE_KEY="errcode";privatestaticfinalintERR_CODE_SUCCESS_VALUE=0;privatestaticfinalStringERR_MSG_KEY="errmsg";/***OkHttp3实例*/privateOkHttp3UtilsokHttp3Utils=newOkHttp3Utils();/***构造方法**@paramdefaultPushUrl默认消息推送地址*@paramjobName名称*@paramgsongson*@paramwxEnterpriseRobotPushUrl企业微信消息推送地址,参考地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/send*/publicEnterpriseWeChatRobotMessageSendServiceImpl(StringdefaultPushUrl,StringjobName,Gsongson,StringwxEnterpriseRobotPushUrl){this.defaultPushUrl=defaultPushUrl;this.jobName=jobName;this.gson=gson;this.wxEnterpriseRobotPushUrl=wxEnterpriseRobotPushUrl;}/***构造方法**@paramdefaultPushUrl默认消息推送地址*@paramjobName名称*@paramgsongson*/publicEnterpriseWeChatRobotMessageSendServiceImpl(StringdefaultPushUrl,StringjobName,Gsongson){this.defaultPushUrl=defaultPushUrl;this.jobName=jobName;this.gson=gson;this.wxEnterpriseRobotPushUrl=null;}/***推送消息**@parammsg消息体*@return返回推送结果*/@OverridepublicResultDTOsendMassage(EnterpriseRobotMessageDOmsg){try{StringmsgStr=gson.toJson(msg);log.info("收到企业微信推送请求,job:{};开始推送消息:{}",jobName,msgStr);StringpushUrl=getPushUrl(msg);log.debug("推送地址:{}",pushUrl);//推送消息StringresultStr=okHttp3Utils.post(pushUrl,msgStr);//记录结果并返回log.info("推送结果:{}",resultStr);JSONObjectresultJson=JSONObject.parseObject(resultStr);booleansuccess=resultJson.getInteger(ERR_CODE_KEY)==ERR_CODE_SUCCESS_VALUE;StringresMsg=resultJson.getString(ERR_MSG_KEY);if(!success){SkynetUtils.printError(String.format("消息推送失败:%s",resMsg),MODULE,CATEGORY,"消息推送失败",jobName,"",null);returnnewResultDTO(success,"发送消息成功",resultJson.getString(ERR_CODE_KEY),resMsg);}returnnewResultDTO(success,"发送消息成功","200",resMsg);}catch(Exceptione){StringerrMsg=String.format("消息推送异常:%s",e.getMessage());SkynetUtils.printError(errMsg,MODULE,CATEGORY,"消息推送异常",jobName,"",e);returnnewResultDTO(false,"消息发送异常","999",errMsg);}}/***获取推送的url地址(带key参数)**@parammsg入参*@return返回地址;如果无法组成地址,则返回null*/privateStringgetPushUrl(EnterpriseRobotMessageDOmsg){Stringkey=msg.getRobotKey();if(!StringUtils.isEmpty(key)&&!StringUtils.isEmpty(wxEnterpriseRobotPushUrl)){finalStringurlKey="key";returnwxEnterpriseRobotPushUrl+"?"+urlKey+"="+key;}elseif(!StringUtils.isEmpty(defaultPushUrl)){returndefaultPushUrl;}else{thrownewIllegalInputVariableException("入参中key为空或企业微信机器人推送地址为空,且任务未配置默认推送地址,无法组合出推送地址,请检查代码!");}}}EnterpriseRobotMessageDO消息实体类,用于构建Robot消息使用建造者模式,在具体使用过程时根据不同需求选择构建不同的Builderpackagedemo.model.wechat;importlombok.AccessLevel;importlombok.AllArgsConstructor;importlombok.Getter;importlombok.ToString;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Collections;importjava.util.List;/***EnterpriseRobotMessageDO*企业微信机器人消息推送实体**@authorJohnChen*@since2019/12/12*/@ToStringpublicclassEnterpriseRobotMessageDO{publicfinalstaticStringMSG_TYPE_TEXT="text";publicfinalstaticStringMSG_TYPE_MARKDOWN="markdown";publicfinalstaticStringMSG_TYPE_IMAGE="image";publicfinalstaticStringMSG_TYPE_NEWS="news";/***推送robotkey(不填则由实现决定如何处理)*优先使用pushKey*/@GetterprivateStringrobotKey;/***消息类型枚举*/privateStringmsgtype;//region不同消息类型用到的不同字段。每次仅需要实例化1个即可/***type=text时需要去构建的实体*/privateTextTypetext;/***type=markdown时需要去构建的实体*/privateMarkdownTypemarkdown;/***type=image时需要去构建的实体*/privateImageTypeimage;/***type=news时需要去构建的实体*/privateNewsTypenews;//endregion/***构建一个Text类型消息实体Builder**@paramcontent消息内容*@return返回builder*/publicstaticTextBuildertextBuilder(Stringcontent){returnnewTextBuilder(content);}/***构建一个Markdown类型消息实体**@paramcontent消息内容(Markdown格式)*@return返回builder*/publicstaticMarkdownBuildermarkdownBuilder(Stringcontent){returnnewMarkdownBuilder(content);}/***构建一个Image类型消息实体**@parambase64图片内容的base64编码;无需增加类似data:image/png;base64,的头。这一点要注意,因为在线转换工具大多会带上这个前缀*@parammd5图片内容(base64编码前)的md5值;*@return返回builder*/publicstaticImageBuilderimageBuilder(Stringbase64,Stringmd5){returnnewImageBuilder(base64,md5);}/***构建一个news类型消息实体**@paramtitle标题,不超过128个字节,超过会自动截断*@paramurl点击后跳转的链接。*@paramdescription描述,不超过512个字节,超过会自动截断非必填*@parampicUrl图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图1068*455,小图150*150。非必填*@return返回builder*/publicstaticNewsBuildernewsBuilder(Stringtitle,Stringurl,Stringdescription,StringpicUrl){returnnewNewsBuilder(title,url,description,picUrl);}publicstaticNewsBuildernewsBuilder(Stringtitle,Stringurl,Stringdescription){returnnewNewsBuilder(title,url,description,null);}publicstaticNewsBuildernewsBuilder(Stringtitle,Stringurl){returnnewNewsBuilder(title,url,null,null);}//region消息实体类@AllArgsConstructorprivatestaticclassTextType{/***文本内容,最长不超过2048个字节,必须是utf8编码*/privateStringcontent;/***userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list*/privateListmentioned_list;/***手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人*/privateListmentioned_mobile_list;}@AllArgsConstructorprivatestaticclassMarkdownType{/***markdown内容,最长不超过4096个字节,必须是utf8编码*/privateStringcontent;}@AllArgsConstructorprivatestaticclassImageType{/***图片内容的base64编码*/privateStringbase64;/***图片内容(base64编码前)的md5值*/privateStringmd5;}@AllArgsConstructorprivatestaticclassNewsType{/***图文消息,一个图文消息支持1到8条图文*/privateListarticles;/***图文消息实体*/@AllArgsConstructorprivatestaticclassArticle{/***标题,不超过128个字节,超过会自动截断*/privateStringtitle;/***描述,不超过512个字节,超过会自动截断*非必填*/privateStringdescription;/***点击后跳转的链接。*/privateStringurl;/***图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图1068*455,小图150*150。*非必填*/privateStringpicurl;}}//endregion//region各类构造方法,用于构建不同的消息类型实体privateEnterpriseRobotMessageDO(NewsTypenews){this.msgtype=MSG_TYPE_NEWS;this.news=news;}privateEnterpriseRobotMessageDO(NewsTypenews,StringrobotKey){this.msgtype=MSG_TYPE_NEWS;this.news=news;this.robotKey=robotKey;}privateEnterpriseRobotMessageDO(ImageTypeimage){this.msgtype=MSG_TYPE_IMAGE;this.image=image;}privateEnterpriseRobotMessageDO(ImageTypeimage,StringrobotKey){this.msgtype=MSG_TYPE_IMAGE;this.image=image;this.robotKey=robotKey;}privateEnterpriseRobotMessageDO(MarkdownTypemarkdown){this.msgtype=MSG_TYPE_MARKDOWN;this.markdown=markdown;}privateEnterpriseRobotMessageDO(MarkdownTypemarkdown,StringrobotKey){this.msgtype=MSG_TYPE_MARKDOWN;this.markdown=markdown;this.robotKey=robotKey;}privateEnterpriseRobotMessageDO(TextTypetext){this.msgtype=MSG_TYPE_TEXT;this.text=text;}privateEnterpriseRobotMessageDO(TextTypetext,StringrobotKey){this.msgtype=MSG_TYPE_TEXT;this.text=text;this.robotKey=robotKey;}//endregion//region不同消息类型的Builder/***Text类型消息Builder*/publicstaticclassTextBuilder{/***当需要@all时候需要填入mentioned_list或mentioned_mobile_list中的*/privatestaticfinalStringAT_ALL="@all";privateStringcontent;privateListmentionedList;privateListmentionedMobileList;/***构造方法,消息体必填**@paramcontent消息体*/privateTextBuilder(Stringcontent){this.content=content;}/***添加userId,用于在消息中@某人**@parammentioned企业微信userId*@return返回建造者本身*/publicTextBuilderaddUserIdForAt(String...mentioned){if(mentioned!=null&&mentioned.length>0){if(mentionedList==null){mentionedList=newArrayList();}mentionedList.addAll(Arrays.asList(mentioned));}returnthis;}/***添加手机号,用于添加某人*当无法获取到userId的时候,则可以添加手机号(需要是企业微信绑定的)**@parammobiles企业微信userId*@return返回建造者本身*/publicTextBuilderaddMobileForAt(String...mobiles){if(mobiles!=null&&mobiles.length>0){if(mentionedMobileList==null){mentionedMobileList=newArrayList();}mentionedMobileList.addAll(Arrays.asList(mobiles));}returnthis;}publicTextBuilderatAll(){addMobileForAt(AT_ALL);returnthis;}publicEnterpriseRobotMessageDObuild(){returnnewEnterpriseRobotMessageDO(newTextType(content,mentionedList,mentionedMobileList));}publicEnterpriseRobotMessageDObuild(StringrobotKey){returnnewEnterpriseRobotMessageDO(newTextType(content,mentionedList,mentionedMobileList),robotKey);}}/***Markdown类型消息Builder*/@AllArgsConstructor(access=AccessLevel.PRIVATE)publicstaticclassMarkdownBuilder{/***markdown内容,最长不超过4096个字节,必须是utf8编码*/privateStringcontent;publicEnterpriseRobotMessageDObuild(){returnnewEnterpriseRobotMessageDO(newMarkdownType(content));}publicEnterpriseRobotMessageDObuild(StringrobotKey){returnnewEnterpriseRobotMessageDO(newMarkdownType(content),robotKey);}}/***Image类型消息Builder*/@AllArgsConstructor(access=AccessLevel.PRIVATE)publicstaticclassImageBuilder{/***图片内容的base64编码*/privateStringbase64;/***图片内容(base64编码前)的md5值*/privateStringmd5;publicEnterpriseRobotMessageDObuild(){returnnewEnterpriseRobotMessageDO(newImageType(base64,md5));}publicEnterpriseRobotMessageDObuild(StringrobotKey){returnnewEnterpriseRobotMessageDO(newImageType(base64,md5),robotKey);}}/***News类型消息Builder*/publicstaticclassNewsBuilder{/***图文消息,一个图文消息支持1到8条图文*/privateListarticles;/***构造方法**@paramtitle标题*@paramurl跳转地址*@paramdescription描述(非必填)*@parampicUrl图片地址(非必填)*/privateNewsBuilder(Stringtitle,Stringurl,Stringdescription,StringpicUrl){this.articles=newArrayList(Collections.singletonList(newNewsType.Article(title,description,url,picUrl)));}/***新增一个图文**@paramtitle标题*@paramurl跳转地址*@paramdescription描述(可为空)*@parampicUrl图片地址*@return返回builder*/publicNewsBuilderaddArticles(Stringtitle,Stringurl,Stringdescription,StringpicUrl){articles.add(newNewsType.Article(title,description,url,picUrl));returnthis;}/***新增一个图文**@paramtitle标题*@paramurl跳转地址*@paramdescription描述(可为空)*@return返回builder*/publicNewsBuilderaddArticles(Stringtitle,Stringurl,Stringdescription){returnaddArticles(title,url,description,null);}/***新增一个图文**@paramtitle标题*@paramurl跳转地址*@return返回builder*/publicNewsBuilderaddArticles(Stringtitle,Stringurl){returnaddArticles(title,url,null,null);}publicEnterpriseRobotMessageDObuild(){returnnewEnterpriseRobotMessageDO(newNewsType(articles));}publicEnterpriseRobotMessageDObuild(StringrobotKey){returnnewEnterpriseRobotMessageDO(newNewsType(articles),robotKey);}}//endregion}测试Demo(使用方式)这里通过几个测试Demo来说明一下使用方法
服务Bean构建下面的案例使用了Spring框架先构建了一个Bean也可以不通过Spring框架,直接new一个实现packagedemo.nelsen.config;importcom.google.gson.Gson;importdemo.common.enumlibrary.tccomponent.EnumAirtravelOpsMonitorDataNelsenKeys;importdemo.service.wechat.impl.EnterpriseWeChatRobotMessageSendServiceImpl;importdemo.service.wechat.inter.EnterpriceWechatRobotMessageSendService;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;/***WechatSendMessageConfig*企业微信消息推送Bean**@authorJohnChen*@since2019/12/13*/@ConfigurationpublicclassWechatSendMessageConfig{/***企业微信机器人公共推送服务*注意:使用这个Bean的时候,在发送时需要带上对应的机器人Key,否则会导致发送到默认机器人上**@paramgsongson*@return返回bean*@throwsException获取统一配置时可能出现的错误*/@BeanpublicEnterpriceWechatRobotMessageSendServicepublicWxRobotSendService(Gsongson)throwsException{returnnewEnterpriseWeChatRobotMessageSendServiceImpl("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8606ac1f-dc01-423s3-9d74-121343ef539","企业微信Robot公共推送服务",gson,"https://qyapi.weixin.qq.com/cgi-bin/webhook/send");}}测试用例下面的测试用例分别构建了4中不同的消息类型进行推送
如果不想使用Spring框架,可以直接new一个EnterpriceWechatRobotMessageSendServicepackagedemo.nelsen.tests;importdemo.common.utls.EncodeUtils;importdemo.model.wechat.EnterpriseRobotMessageDO;importdemo.service.wechat.inter.EnterpriceWechatRobotMessageSendService;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;importjava.io.IOException;/***WechatSendMessageConfig*微信发送消息测试用例集**@authorJohnChen*@since2019/12/13*/@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.DEFINED_PORT)publicclassWechatSendMessageTests{@AutowiredprivateEnterpriceWechatRobotMessageSendServicepublicWxRobotSendService;@TestpublicvoidtiantiUploadSendTest(){/*文本消息类型*/EnterpriseRobotMessageDOmessageTextDO=EnterpriseRobotMessageDO.textBuilder("nelsen测试用例中-上传动态-测试用例-test消息").addMobileForAt("13800000000").build();asserttianTiUploadRobotSendService.sendMassage(messageTextDO).getSuccess();/*markdown类型消息*/EnterpriseRobotMessageDOmessageMarkdownDo=EnterpriseRobotMessageDO.markdownBuilder("#消息发送测试 "+">小鲜肉最牛 "+">消息来自上传动态-Markdown类型消息").build();asserttianTiUploadRobotSendService.sendMassage(messageMarkdownDo).getSuccess();/*图片类型*/StringimageB="data:image/gif;base64,R0lGODlhdQAmAKIAAOYyL+rU4llg6Jmd8e92dCky4eEGAv///yH5BAAAAAAALAAAAAB1ACYAAAP/eLrc/jC2IEoZMATJu/9gyFVWIUyksIls677LUJbrEcxWDe98f+CWk4I0w/iOSNANKJQBC8mo9LEEDp8F3XR7rOIU2Cx3jHwKsUKyWqRhEEvGN3xN91BoCq8l9tTW/244Rk4mOkBGgIl8VjF+d4V5A5KKf3IWiCCEOZRraGxPnGqeIZpzoVyjDBptDpYmp1yumI9BWq5QUQS6fn+lm3lYmLdSBsW8xcjJBgAABLwfAMhXRQt6ODWuJ8rb3AYK0d3h4OHIBC7jC0TCYb/ZB+Th3/Dc4/PmLOgSvkwK+xjMAAEmCwhQnrc8rFSxGkiwnoFnEnTp0mdqlJw0DgIggwgu/08xABM+KkhGJdm9JJqMhGkyQIAATA3HxVwATuO8jyQfECiXJyYzZwzyOSilcqWEmzkP1ES6LGmDcStshjspNJAgMOwwNmBajGZXGyJVhU22quxOaR7hrahqQ52DC1jh/nj1AG0Eux3BLgu59x3SkxHO9mVLhJfcirUc2IWA96vUbvf+QkNbldCgIGCKHi6h1e/Bu1+VOiYHYAVTwBHyCfWyocoZzZovbf3ok1njzx/IltUg+GG13twMfta0gdDruLH3MOD6uaPDcI8hCAZp4/k24elmtOacGXlcPAuYe/VmnVt0ncioTyeYU+ibQRWOHyCRXEz40BDyivYWYBzQAIbTbeBUNTIZRF1QlNlVimGdfffLSPg9oJ9+vVV4H23smWSgFqrZZc0AWiTWACMXcjAhfgAqgxpXKwDXDXbZsSPjVcth6NNtZj3nzCpM1dDfPDACM+OQ9l2I1HiPlfNceentMoGLygQp5Eq3aKfYaUgOdBIB9RyYSEsmvASiFgG0RMRLEK2RAAA7";EnterpriseRobotMessageDOmessageImageDo=EnterpriseRobotMessageDO.imageBuilder(imageB,EncodeUtils.md5bytes(EncodeUtils.decodeBase64(imageB))).build();asserttianTiUploadRobotSendService.sendMassage(messageImageDo).getSuccess();/*图文消息类型*/EnterpriseRobotMessageDOmessageNewsDO=EnterpriseRobotMessageDO.newsBuilder("上传动态-news类型消息-跳转","https://www.baidu.com/","上传动态-news类型消息-测试用例1","http://img1.imgtn.bdimg.com/it/u=3334640638,1744228669&fm=26&gp=0.jpg").addArticles("上传动态-news类型消息-跳转TCSchedule","https://www.baidu.com/","上传动态-news类型消息-测试用例2","http://img1.imgtn.bdimg.com/it/u=3334640638,1744228669&fm=26&gp=0.jpg").build();asserttianTiUploadRobotSendService.sendMassage(messageNewsDO).getSuccess();}}