Create a free message reminder Push mechanism through corporate WeChat and WeChat

I have done a lot of services myself, and I want to send them to mobile phones by sending Push messages. However, it is necessary to develop corresponding apps for mobile phones of different platforms, and the cost is relatively high. So I started to look for an open source and free message reminder mechanism.

direction:

1. Push service

2. WeChat official account

3. Calendar synchronization

After a fierce investigation, it was found that:

Push services are basically required to develop corresponding Apps

If the WeChat official account pushes messages, the number of messages that can be pushed every day is limited, or template messages are used, which is relatively poor in scalability

The calendar synchronization is relatively low, and the corresponding synchronization software needs to be installed on the mobile phone, which is not universal.

Later, it was discovered that Enterprise WeChat supports custom applications and opened up WeChat, so we have a new direction:

1. Register a business

2. Create a new application under the enterprise

3. Use WeChat to bind enterprise WeChat

4. Turn on the switch of enterprise WeChat message synchronization to WeChat

5. Connect your own reminder/alarm service to the application in the enterprise WeChat

6. Add people (family, friends) who need to receive information to corporate members

Finally achieve the following effect

Attach a small code for weather forecast reminder:

# -*- 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)

Guess you like

Origin blog.csdn.net/u013772433/article/details/122827298