1> 注册配置 IFTTT
首先注册一个IFTTT账号 (https://ifttt.com).
登录进入页面后点击右上角create,准备新建一个applet.
![图片[1]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic01.png)
进去后点击 + this, 如图。
![图片[2]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic02.png)
搜索 webhooks.
![图片[3]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic03.png)
进去后选择Receive a web request, 这个trigger能够使得这个webhooks收到一个http请求后触发一个事件。
![图片[4]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic04.png)
编写该 trigger 的名称:
![图片[5]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic05.png)
然后点击 that.
![图片[6]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic06.png)
搜索 notification.
![图片[7]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic07.png)
选择 send a notification from the ifttt app. 这个action能够使得ifttt发出一个通知。
![图片[8]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic08.png)
里面可以设置消息的格式
其中:{{EventName}}是我们前面设定的事件名称,而Add ingredient里面的value1、value2、value3则是服务器端发送http请求时带的参数。
![图片[9]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic09.png)
可以设置成如下的格式:
![图片[10]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic10.png)
Finish!
![图片[11]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic11.png)
2. 编写Python 通知脚本
进入 https://ifttt.com/maker_webhooks 页面,你可以看见你刚新建的webhooks.
点击右上角的Documentation.
![图片[12]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic12.png)
你就可以看见你的应用的Key,这个Key很重要,将用来发送请求
webhooks的调用方式就是通过发送POST或GET请求到下面这个网址:
https://maker.ifttt.com/trigger/你的event_name/with/key/你的Key
其中,你还可以带三个参数,以json格式放在body中,如 {“value1”: “这是一个测试”},最终通知里的Value1会被这个value1替代。
![图片[13]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic13.png)
制作通知脚本,例如新建一个文件叫 notice.py 如下,text可以放你想发送的文字。
你可以把这个文件放在你本机上,也可以放在服务器上结合某种功能。
记得先在手机上先下载好IFTTT并登陆。
class Notice(object):
'''
通过 ifftt 发送手机通知
'''
def __init__(self):
self.key = 'bPCYm3o8Nm-3TfcN_5P2fX' # IFTTT 私钥
self.wea_key = 'SJcIp4Pwp5qpoUe2x'
self.event_name = 'weather'
self.location = '36.11:120.43'
def send_notice(self, text, temp, time):
url = 'https://maker.ifttt.com/trigger/{}/with/key/{}'
payload = {"value1": text, "value2": temp, "value3": time}
headers = {"Content-Type": "application/json"}
response = requests.request("post", url.format(self.event_name, self.key), data=json.dumps(payload), headers=headers)
print(response.text)
![图片[14]-N13※python 发送手机通知-霜林轩](https://www.chiles.top/assets/pic/ifttt/pic15.png)