使用 #feilong# https://www.oschina.net/p/feilong 发企业微信机器人
企业微信机器人在日常的使用场景中越来越多, 比如服务器关键任务告警,定时发通知提醒等等, 和短信相比即免费又不像短信逐渐只有接收验证码的功能, 那么问题来了,如何使用feilong来发送企业微信机器人呢?
简单 3 步
第1步: jar 依赖必要依赖
com.github.ifeilonggroupId>
feilongartifactId>3.0.9version>
dependency>org.apache.httpcomponentsgroupId>
httpmimeartifactId>4.5.12version>
httpclientartifactId>org.apache.httpcomponentsgroupId>
exclusion>
exclusions>
dependency>org.apache.httpcomponentsgroupId>
httpclientartifactId>4.5.12version>
dependency>org.springframeworkgroupId>
spring-beansartifactId>5.2.7.RELEASEversion>
dependency>org.springframeworkgroupId>
spring-contextartifactId>5.2.7.RELEASEversion>
dependency>
for test 和日志
第2步: 使用xml 配置机器人org.springframeworkgroupId>
spring-testartifactId>5.2.7.RELEASEversion> testscope>
dependency>org.slf4jgroupId>
slf4j-apiartifactId>1.7.30version>
dependency>org.slf4jgroupId>
slf4j-log4j12artifactId>1.7.30version>
dependency>log4jgroupId>
log4jartifactId>1.2.17version>
dependency>junitgroupId>
junitartifactId>4.13version> testscope>
dependency>org.hamcrestgroupId>
hamcrest-libraryartifactId>1.3version> testscope>
dependency>
wxbot.xml
注意xml中需要使用feilong 的customer xml tag
feilong:wxworkBot 只有一个核心自定义属性, key 是企业微信机器人对应的key , key 参考 如何配置群机器人?
第3步: 使用
beans>
简单小示例:
package com.feilong.namespace;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import com.feilong.net.wxwork.bot.WxworkBot;
import com.feilong.net.wxwork.bot.message.WxworkResponse;
@ContextConfiguration(locations = { "classpath*:wxbot.xml" })
public class WxworkBotTagTest extends AbstractJUnit4SpringContextTests{
@Autowired
@Qualifier("wxworkBot")
private WxworkBot wxworkBot;
//---------------------------------------------------------------
@Test
public void test(){
WxworkResponse wxworkResponse = wxworkBot.sendMessage("hello world");
assertThat(
wxworkResponse,
allOf(
hasProperty("isSuccess", is(true)), //
hasProperty("errcode", is("0"))));
}
}
执行这个单元测试, 你将会收到企业微信机器人发的消息
关于 WxworkBotcom.feilong.net.wxwork.bot.WxworkBot 提供了2个易用的方法
sendMessage(String) 发送普通消息,以及Markdown格式的消息
sendNewsMessage(Article...) 发送推送图文类型消息
在上述 hello world示例中, 你可以看到发送简单消息, 也支持markdown格式的消息, 比如
@Test
public void test(){
wxworkBot.sendMessage(
"实时新增用户反馈,请相关同事注意。n" + "> 类型:n"
+ "> 普通用户反馈:n"
+ "> VIP用户反馈:");
}
效果:
sendNewsMessage(Article...) 发送推送图文类型消息你还可以使用sendNewsMessage 来推送图文类型的消息,
package com.feilong.namespace;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import com.feilong.net.wxwork.bot.WxworkBot;
import com.feilong.net.wxwork.bot.message.news.Article;
@ContextConfiguration(locations = { "classpath*:wxbot.xml" })
public class WxworkBotTagNewsMessageTest extends AbstractJUnit4SpringContextTests{
@Autowired
@Qualifier("wxworkBot")
private WxworkBot wxworkBot;
@Test
public void test(){
//是 标题,不超过128个字节,超过会自动截断
String title = "提醒您,点击填Timesheet";
//描述,不超过512个字节,超过会自动截断
String description = "@all 点我直接填写Jira Timesheet" + "nn" + "我爱工作,工作使我快乐";
//"点我直接填写Jira Timesheet",
String url = "http://jira.xxx.cn/plugins/servlet/aio-ts/bridge/pages/aiotimeentry";
String img = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1585320605587&di=9424f8862476b2ce819ac9f5637567b1&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F503d269759ee3d6d55e89bf048166d224f4adeda.jpg";
wxworkBot.sendNewsMessage(new Article(title, description, url, img));
}
}
效果:
--完



