自己做了很多服务,想通过发Push消息的方式发送到手机上,奈何针对不同平台的手机需要对应开发相应的App,成本较高。所以开始寻求有没有开源免费的消息提醒机制。
方向:
1. Push服务
2. 微信公众号
3. 日历同步
经过一顿操作猛如虎的调研发现:
Push服务基本都需要来开发对应App
微信公众号推送消息的话每天能推送的消息是有限的,或者使用模板消息,相对的扩展性较差
日历同步的话就比较Low了,也需要手机上安装上相应的同步软件,并不普世。
后来发现企业微信支持了自定义应用并且打通了微信,于是乎咱有了新方向:
1. 注册一个企业
2. 在企业下新建一个应用
3. 使用微信绑定企业微信
4. 打开企业微信消息同步到微信的开关
5. 将自己的提醒/报警服务接到企业微信中的应用
6. 将需要收到信息的人(家人、朋友)加入到企业成员
最后实现以下效果
附上一个天气预报提醒的小代码:
# -*- coding: utf-8 -*-
# @Time : 2021/6/21 5:10 下午
# @Author : SunRuichuan
# @File : GetWeather.py
import datetime
import json
import requests
appid = '*****'
appsecret = '*****'
weather_url = 'https://tianqiapi.com/api'
def getWeather(city='北京'):
params = {
'appid': appid,
'appsecret': appsecret,
'version': 'v1',
'cityid': '',
'city': city,
'ip': '',
}
return requests.get(url=weather_url, params=params)
def getAccessToken():
url = '企业微信获取token链接,自己去申请'
res = requests.get(url).json()
if res['errcode'] == 0:
return res['access_token']
else:
return None
def sendMsgToAll(content):
"""通过企业微信申请的应用发送内容"""
access_token = getAccessToken()
if access_token is None:
return
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
data = {
"touser": '@all',
"msgtype": "text",
"agentid": 1000002,
"text": {
"content": content
},
"safe": 0
}
requests.post(url=url, data=json.dumps(data))
def getCarLimit():
tomorrow = (datetime.date.today() + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
# print(tomorrow)
url = 'http://yw.jtgl.beijing.gov.cn/jgjxx/services/getRuleWithWeek'
result = requests.get(url=url).json()
for i in result['result']:
_date = str(i['limitedTime']).replace('年', '-').replace('月', '-').replace('日', '')
if tomorrow == _date:
# print(i['limitedNumber'])
return i['limitedNumber']
if __name__ == '__main__':
limit = getCarLimit()
limit_msg = ''
if limit is not None and limit != '不限行':
limit_msg = f'明日限行尾号为【{limit}】'
print(limit_msg)
weather_res = getWeather().json()
city_name = weather_res['city']
update_time = weather_res['update_time']
weather_data = weather_res['data']
'''获取明日天气'''
date = weather_data[1]['date']
weather = weather_data[1]['wea']
high_tem = weather_data[1]['tem1']
low_tme = weather_data[1]['tem2']
air_level = weather_data[1]['air_level']
wash_car = '未知'
for wash in weather_data[1]['index']:
if wash['title'] == '洗车指数':
wash_car = wash['level']
weather_string = f'{date} {city_name}\n天气情况【{weather}】\n最高气温【{high_tem}】\n最低气温【{low_tme}】\n空气质量【{air_level}】\n洗车指数【{wash_car}】'
print(weather_string)
msg = weather_string + '\n' + limit_msg
sendMsgToAll(msg)