How to make a WeChat/Slack chatbot
WeChat Work and Slack bot template
This is a template for a WeChat and Slack bot that can be used to send daily, weekly, or spontaneous reminders for meetings. It is an example of a cron job, and it uses webhook principles to send notifications to the apps.
It uses a simple SCF function with a timer to automatically trigger the function at the specified time (cron job). When creating a timer trigger, you can customize the triggering time by using a standard cron expression. More documentation on cron expressions syntax and input parameters of time triggers can be found here.
The SCF function also has an api gateway url through which spontaneous meetings can be scheduled. Details for the meeting can be added via a query string parameter "info", for example https://service-xxx.gz.apigw.tencentcs.com/release/spontaneous_meeting?info=organized%20by%20Jason
Prepare
Before all below steps, you should install Serverless Framework globally:
$ npm i serverless -g
Download
Severless cli is very convenient, it can download templates in any github
project which should contain serverless.yml
file.
$ serverless init -t bot-template
You can also download the files from the Github repo.
Bootstrap
Copy .env.example
file to .env
in project root:
Add the access keys of a
Tencent CAM Role with
AdministratorAccess
in the .env
file, like below:
# .env
# Add this line if you have a Tencent international account (Non-Mainland China users)
SERVERLESS_PLATFORM_VENDOR=tencent
# secret for credential
TENCENT_SECRET_ID=xxx
TENCENT_SECRET_KEY=xxx
# global config
REGION=ap-guangzhou
# webhooks for wechat and slack bot
WEBHOOK1=https://qyapi.weixin.qq.com/xxx
WEBHOOK2=https://hooks.slack.com/xxx
For WeChat Work, you can simply add a "Group Robot" to a WeChat group, then copy the Webhook URL link and add it to "WEBHOOK1" in your .env file.
For Slack, you can follow the steps here to add a bot to a channel. Similarly, copy the Webhook URL link and add it to "WEBHOOK2" in your .env file.
You also need to download the "urllib" Python library which is used in the main handler to open and read urls.
$ pip install urllib3
Support commands
Deploy (you can use "--debug" parameter to see what is happening under the hood):
$ sls deploy
serverless ⚡ framework
Action: "deploy" - Stage: "dev" - App: "meeting_bot" - Instance: "meeting_bot"
functionName: meeting_bot
description: sends regular/spontaneous reminders for meetings
namespace: default
runtime: Python3.6
handler: index.main_handler
memorySize: 64
lastVersion: $LATEST
traffic: 1
triggers:
apigw:
- https://service-xxx.gz.apigw.tencentcs.com/release/spontaneous_meeting
timer:
- daily_meeting
- weekly_meeting
Full details: https://serverless.cloud.tencent.com/instances/meeting_bot%3Adev%3Ameeting_bot
Remove:
$ sls remove
serverless ⚡ framework
Action: "remove" - Stage: "dev" - App: "meeting_bot" - Instance: "meeting_bot"
9s › meeting_bot › Success
License
MIT