Now many enterprises are using wechat or Dingding for work communication. We can add a customized group robot to the group to send some reminder or consulting information regularly. It can serve as a group hand and add a little fun to the work.

Group of robots

The following is the document of enterprise wechat and Dingding group robots:

Enterprise wechat group robot document

Nail swarm robot document

Their functions and usage are basically the same, this paper will take the enterprise wechat as an example to explain.

After we add a robot to the group, we can get the Webhook of the robot:

By making a POST request to this address, the robot can send messages in the group.

Swarm bots can only be added in a group, but here’s a trick: We can start a group and then kick everyone else out, so we have a group of our own. This will make it easy for us to test, or create a private group hand.

Hello World

Everything started in Hello World, and swarm robots are no exception. It’s all about making a POST request in whatever language you like.

Using Node.js as an example, I added a dependency on the request library axios as follows:

const axios = require('axios')

axios.post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx', {
        "msgtype": "text"."text": {
            "content": "Hello World"
        }
    })
    .then(response= > {
        console.log(response.data);
    })
    .catch(error= > {
        console.log(error)
    })
Copy the code

If not, run this code and you will see the message sent by the robot in the group:

Zhihu daily

Once we’re sure we can deliver, we can start developing more gameplay. For example, by calling some open interfaces, the data is obtained and forwarded to the group.

Taking Zhihu Daily as an example, the code is as follows:

const axios = require('axios')

postZhiHu()

function postZhiHu() {
    axios.get('https://news-at.zhihu.com/api/4/news/latest')
        .then(response= > {
            var stories = response.data.stories
            postMessage(stories)
        })
        .catch(error= > {
            console.log(error); })}function postMessage(stories) {
    var articles = []
    stories.forEach(story= > {
        articles.push({
            "title": story.title,
            "url": story.url,
            "picurl": story.images[0]
        })
    })
    axios.post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx', {
            "msgtype": "news"."news": {
                "articles": articles
            }
        })
        .then(response= > {
            console.log(response.data);
        })
        .catch(error= > {
            console.log(error); })}Copy the code

Run the code above to receive the zhihu daily sent by the robot:

The robot can send various types of messages. Zhihu Daily here is graphic and graphic. You can see the official documents about the specific types.

Timing task

Naturally, we want robots to be able to send messages automatically, which is where timed tasks come in.

On Node.js you can implement scheduled tasks using schedule:

const schedule = require('node-schedule')

schedule.scheduleJob({hour: 9, minute: 30}, function(){
    postZhiHu()
})
Copy the code

The code above enables the robot to send a message from Zhihu Daily at 9:30 am every day.

But in order to ensure that the message can be received every day, the computer needs to keep running this scheduled task, shutdown or standby time will not receive messages. You can run the task on a server, or you can use the method shown below.

Cloud function

The cloud function service provided by Tencent Cloud allows us to run codes under specified conditions, which is particularly suitable for the use of our group robots. The free amount provided is more than enough for the group robots. The specific use method of cloud function, you can see the official document:

Cloud function documentation

The following is a brief talk about using cloud functions to achieve group robots.

Depend on the configuration

We still use Node.js as the runtime environment. We can use templates to create cloud functions, but if we are adding third-party dependencies, we need to upload the node_modules folder when creating cloud functions.

When creating a cloud function, select “Upload zip package locally” as the submission method:

Then upload the index.js file and node_modules folder as zip:

This makes it fun to use third-party dependencies in cloud functions.

Function code

Main_handler is the entry to the cloud function. We will add the following code to the index.js of the cloud function by modifying the above code:

const axios = require('axios')

exports.main_handler = (event, context, callback) = > {
    axios.get('https://news-at.zhihu.com/api/4/news/latest')
        .then(response= > {
            stories = response.data.stories
            postMessage(stories)
        })
        .catch(error= > {
            console.log(error); })}function postMessage(stories) {
    var articles = []
    stories.forEach(story= > {
        articles.push({
            "title": story.title,
            "url": story.url,
            "picurl": story.images[0]
        })
    })
    axios.post('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx', {
            "msgtype": "news"."news": {
                "articles": articles
            }
        })
        .then(response= > {
            console.log(response.data);
        })
        .catch(error= > {
            console.log(error); })}Copy the code

Click “Save” below, and then click “Test” to see the robot’s Zhihu daily news. It has the same effect as local execution.

triggered

Add trigger methods to cloud functions to make them automatically execute under specified conditions.

Let’s add a trigger that uses a timed trigger. For example, if I want to send a message at 9:30 a.m. every day from Monday to Friday, I can set it like this:

For details on how to use Cron expressions, see the documentation:

Timing trigger documentation

More gameplay?

Here, we can basically play with enterprise wechat and Dingding group robot functions. In addition to the zhihu daily mentioned above, we can also add weather, news, dinner reminders, meeting reminders and other news to the robot.

Don’t know what other interesting features you can think of, feel free to leave a comment.