Arduion报警通知,企业微信机器人报警通知
直接调用我这个方法,即可发送通知。
企业微信机器人对接,通过 esp8266 或者 esp32 或者其他开发板发送通知
咱们以 知乎上这篇文章为例,做 8266的 post推送
如何使用群机器人
- 在终端某个群组添加机器人之后,创建者可以在机器人详情页看的该机器人特有的webhookurl。开发者可以按以下说明a向这个地址发起HTTP POST 请求,即可实现给该群组发送消息。下面举个简单的例子.
假设webhook是:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa
特别特别要注意:一定要保护好机器人的webhook地址,避免泄漏!不要分享到github、博客等可被公开查阅的地方,否则坏人就可以用你的机器人来发垃圾消息了。
以下是用curl工具往群组推送文本消息的示例(注意要将url替换成你的机器人webhook地址,content必须是utf8编码):
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "hello world"
}
}'
- 当前自定义机器人支持文本(text)、markdown(markdown)、图片(image)、图文(news)四种消息类型。
- 机器人的text/markdown类型消息支持在content中使用<@userid>扩展语法来@群成员
消息类型及数据格式
文本类型
{
"msgtype": "text",
"text": {
"content": "广州今日天气:29度,大部分多云,降雨概率:60%",
"mentioned_list":["wangqing","@all"],
"mentioned_mobile_list":["13800001111","@all"]
}
}
参数 | 是否必填 | 说明 |
---|---|---|
msgtype | 是 | 消息类型,此时固定为text |
content | 是 | 文本内容,最长不超过2048个字节,必须是utf8编码 |
mentioned_list | 否 | userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list |
mentioned_mobile_list | 否 | 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人 |
#define BLINKER_WIFI
#include <ArduinoJson.h>
#include <Blinker.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
void sendMSG2() {
Blinker.vibrate();
if (WiFi.status() == WL_CONNECTED) {
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
http.begin(client,"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的key");
http.addHeader("Content-Type", "application/json");
DynamicJsonDocument doc(1024);
// Add string to "msgtype"
doc["msgtype"] = "text";
// Add nested object "text"
JsonObject text = doc.createNestedObject("text");
text["content"] = "我睡醒了,我睡醒了";
String httpRequestData;
// Add array to "mentioned_mobile_list"
JsonArray mentioned_mobile_list = text.createNestedArray("mentioned_mobile_list");
mentioned_mobile_list.add("13831111111");
//mentioned_mobile_list.add("@all");
serializeJson(doc, httpRequestData);
int httpResponseCode = http.POST(httpRequestData); //Send the actual POST request
if (httpResponseCode > 0) {
String response = http.getString(); //Get the response to the request
Serial.println(httpResponseCode); //Print return code
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); //Free resources
} else {
Serial.println("Error in WiFi connection");
}
}