> For the complete documentation index, see [llms.txt](https://feilong.gitbook.io/feilong-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://feilong.gitbook.io/feilong-docs/case/wxworkbot.md).

# 使用feilong发企业微信机器人

企业微信机器人在日常的使用场景中越来越多, 比如服务器关键任务告警,定时发通知提醒等等, 和短信相比即免费又不像短信逐渐只有接收验证码的功能, 那么问题来了,如何使用feilong来发送企业微信机器人呢?

简单 3 步

### 第1步: jar 依赖

必要依赖 (注意检查不要和项目已有的依赖重复了)

```xml
    <dependency>
      <groupId>com.github.ifeilong</groupId>
      <artifactId>feilong</artifactId>
      <version>4.5.0</version>
    </dependency>

    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpmime</artifactId>
      <version>4.5.12</version>
      <exclusions>
        <exclusion>
          <artifactId>httpclient</artifactId>
          <groupId>org.apache.httpcomponents</groupId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.12</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.7.RELEASE</version>
    </dependency>
```

for test 和日志 (注意检查不要和项目已有的依赖重复了)

```xml
    <!--下面是for test 和日志-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.7.RELEASE</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.30</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.30</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-library</artifactId>
      <version>1.3</version>
      <scope>test</scope>
    </dependency>
```

### 第2步: 使用xml 配置机器人

wxbot.xml

注意xml中需要使用feilong 的customer xml tag

feilong:wxworkBot 只有一个核心自定义属性, key 是企业微信机器人对应的key , key 参考 [如何配置群机器人？](https://work.weixin.qq.com/help?person_id=1\&doc_id=13376)

```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:feilong="http://www.feilong.com/schema/feilong"

	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.feilong.com/schema/feilong http://www.feilong.com/schema/feilong/feilong.xsd
    ">

	<!-- 务必替换成你们的key-->
	<feilong:wxworkBot id="wxworkBot" key="80fa90*******a48f1d9e27" />

</beans>
```

### 第3步: 使用

简单小示例:

```java

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.bot.wxwork.WxworkBot;
import com.feilong.net.bot.wxwork.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"))));
    }
}


```

执行这个单元测试, 你将会收到企业微信机器人发的消息

![bot221](https://ifeilong.oss-cn-beijing.aliyuncs.com/%E6%88%AA%E5%B1%8F2020-08-03%20%E4%B8%8B%E5%8D%883.33.22.png)

### 关于 WxworkBot

com.feilong.net.bot.wxwork.WxworkBot 提供了2个易用的方法

* sendMessage(String) 发送普通消息,以及Markdown格式的消息
* sendNewsMessage(Article...) 发送推送图文类型消息

#### sendMessage(String) 发送普通消息,以及Markdown格式的消息

在上述 hello world示例中, 你可以看到发送简单消息, 也支持markdown格式的消息, 比如

```java
    @Test
    public void test(){
        wxworkBot.sendMessage(
                        "实时新增用户反馈<font color=\"warning\">132例</font>，请相关同事注意。\n" + "> 类型:<font color=\"comment\">用户反馈</font>\n"
                                        + "> 普通用户反馈:<font color=\"comment\">117例</font>\n"
                                        + "> VIP用户反馈:<font color=\"comment\">15例</font>");
    }
```

效果:

![bot2212](https://ifeilong.oss-cn-beijing.aliyuncs.com/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_f2754001-9493-419d-8a42-39e0525ab7ef.png)

#### sendNewsMessage(Article...) 发送推送图文类型消息

你还可以使用sendNewsMessage 来推送图文类型的消息,

```java

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.bot.wxwork.WxworkBot;
import com.feilong.net.bot.wxwork.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" + "\n\n" + "我爱工作,工作使我快乐";

        //"点我直接填写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));
    }
}


```

效果:

![bot22123](https://ifeilong.oss-cn-beijing.aliyuncs.com/%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_3f50d0cf-98ad-4159-85ae-bda7c9965c18.png)

\--完


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://feilong.gitbook.io/feilong-docs/case/wxworkbot.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
