关于发送通知的内容。我之前几篇文章里说了几种方法:
1、邮箱
文章地址
2、企业微信
文章地址
这里讲另一种方法:IFTTT(IF THIS THEN THAT)
他是国外的一个自动化软件。可以根据设定好的条件执行响应的操作。可以简单理解为:if 8点了 then 手机启动闹铃叫我起床。
可以实现各种预设好了的提醒或者操作。在本篇文章中,我们重点说一下怎么用程序代码调用,并在手机上进行消息提醒。
事先准备:
1、注册IFTTT账号(https://ifttt.com,自己注册)
2、手机上下载一个IFTTT的软件(这貌似是个坑,国内好像不太好找下载点,自己找梯子吧)
步骤:
1、创建与配置
登录IFTTT之后(https://ifttt.com/explore),
1、点:create
2、点:Add
3、搜:webhooks
4、点进去之后,选Receive a web request
5、编写该 trigger 的名称,记到小本本上,暂时命名为:名称A。然后点create tigger
6、点:that里的Add
7、搜:notification
8、选:send a notification from the ifttt app
{{EventName}}就是名称A
Add ingredient里面的value1、value2、value3是三个参数,可以自己指定。
9、点finish,创建结束
10、到这里看刚才创建的webhooks(https://ifttt.com/maker_webhooks)
点Documentnation
11、记录下这个key,很重要。记到小本本上,暂时命名为:秘钥B
12、打开手机上的IFTTT,登录刚才注册的那个账号
2、制作通知脚本
制作通知脚本,例如新建一个文件叫 notice.py 如下。
import requests
import json
import datetime
def send_ifttt_notice(event_name, key, *args):
url = f"https://maker.ifttt.com/trigger/{event_name}/with/key/{key}"
text_list = []
for text in args:
text_list.append(text)
payload = {"value1": text_list[0], "value2": text_list[1], "value3": text_list[2]}
headers = {"Content-Type": "application/json"}
response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
print(text_list)
print(response.text)
Key = '###########'#这里填写上面小本本上记录的:秘钥B
text1 = '出错服务器节点' + 'error_server'
text2 = '出错信息' + 'errormessage'
text3 = '报错时间' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
send_ifttt_notice('my_notice', Key, text1, text2, text3) # 这里第一个参数,就是上边小本本上记录的:名称A
效果如下:
注意:手机上的IFTTT需要后台保活。
以上就讲完了流程操作了。
不过这种办法还是太笨重了,仅作为通知的补充参考手段。