• Python 实现微信测试号情侣纪念消息推送(消息群发)


    概述

    主要实现微信用户关注个人的测试号之后,可以像公众号一样定向的推送消息。该代码主要实现情侣之间提示对方城市的天气信息以及生日、纪念日等信息,此外还可以在这些模板的基础上拓展更多信息,写上你想说的话等。

    总体来说,该代码就是一段用以朋友间玩笑的代码,实现公众号推送的效果。


    实现步骤

    1、打开微信公众平台登录微信号

    微信公众平台

    登录后,如下图所示

    2、让朋友关注该测试号

    3、新建测试模板

    1. 城市:{{parent.DATA}} {{city.DATA}}
    2. 天气:
    3. 今日天气:{{type.DATA}}
    4. 高温:{{tep_high.DATA}}
    5. 低温:{{tep_low.DATA}}
    6. tips:{{notice.DATA}}
    7. 今天是你们的第:{{love_days.DATA}}
    8. 距离生日:{{birthday_left.DATA}}{{words.DATA}}

    4、Python 代码

    注意: 以下是需要填写的内容。

    1. app_id = "" # 在测试号信息一览, app_id
    2. app_s = "" # 在测试号信息一览,app_secret
    3. user_id = [""] # 在测试号二维码一览, user_id 关注的用户微信ID
    4. template_id = "" # 在模板消息接口一览, 生成的模板id, 新建的ID

     完整代码

    1. from datetime import date, datetime
    2. from wechatpy import WeChatClient
    3. from wechatpy.client.api import WeChatMessage, WeChatTemplate
    4. import requests
    5. import random
    6. today = datetime.now() # 获取今日日期
    7. start_date = "2017-11-25" # 恋爱开始时间
    8. city = "101110809" # 城市天气查询的id ,根据自己城市查询城市ID
    9. birthday = "07-14" # 出生日期
    10. app_id = "" # app_id
    11. app_s = "" # appsecret
    12. user_id = [""] # user_id 关注的用户微信ID
    13. template_id = "" # 生成的模板id, 新建的ID
    14. def get_weather():
    15. url = "http://t.weather.sojson.com/api/weather/city/" + city
    16. res = requests.get(url).json()
    17. # res 结果
    18. '''
    19. 'date':'20220827',
    20. 'time':'2022-08-27 13:17:02',
    21. 'cityInfo':{
    22. 'city':'',
    23. 'citykey':'101110809',
    24. 'parent':'',
    25. 'updateTime':'12:16'
    26. },
    27. 'data':{
    28. 'shidu':'86%',
    29. 'pm25':9.0,
    30. 'pm10':0.0,
    31. 'quality':'优',
    32. 'wendu':'27',
    33. 'ganmao':'各类人群可自由活动',
    34. 'forecast':[
    35. {
    36. 'date':'27',
    37. 'high':'高温 26℃',
    38. 'low':'低温 18℃',
    39. 'ymd':'2022-08-27',
    40. 'week':'星期六',
    41. 'sunrise':'06:26',
    42. 'sunset':'19:26',
    43. 'aqi':17,
    44. 'fx':'东风',
    45. 'fl':'1级',
    46. 'type':'小雨',
    47. 'notice':'雨虽小,注意保暖别感冒'
    48. },
    49. {
    50. 'date':'28',
    51. 'high':'高温 19℃',
    52. 'low':'低温 18℃',
    53. 'ymd':'2022-08-28',
    54. 'week':'星期日',
    55. 'sunrise':'06:27',
    56. 'sunset':'19:25',
    57. 'aqi':19,
    58. 'fx':'北风',
    59. 'fl':'2级',
    60. 'type':'大雨',
    61. 'notice':'出门最好穿雨衣,勿挡视线'
    62. },
    63. '''
    64. citys = res['cityInfo']
    65. weather = res['data']['forecast']
    66. return weather, citys
    67. def get_count():
    68. delta = today - datetime.strptime(start_date, "%Y-%m-%d")
    69. return delta.days
    70. def get_birthday():
    71. next = datetime.strptime(str(date.today().year) + "-" + birthday, "%Y-%m-%d")
    72. if next < datetime.now():
    73. next = next.replace(year=next.year + 1)
    74. return (next - today).days
    75. def get_words():
    76. words = requests.get("https://api.shadiao.pro/chp")
    77. if words.status_code != 200:
    78. return get_words()
    79. return words.json()['data']['text']
    80. def get_random_color():
    81. return "#%06x" % random.randint(0, 0xFFFFFF)
    82. client = WeChatClient(app_id, app_secret)
    83. wm = WeChatMessage(client)
    84. weather_list, city_list = get_weather()
    85. # 划分天气信息
    86. print(weather_list)
    87. type = weather_list[0]['type'] # 天气类型
    88. tep_high = weather_list[0]['high'] # 高温
    89. tep_low = weather_list[0]['low'] # 低温
    90. notice = weather_list[0]['notice'] # 提示信息
    91. # 划分城市
    92. parent = city_list['parent']
    93. citys = city_list['city']
    94. data = {"parent":{"value":parent, "color": get_random_color()},
    95. "city":{"value":citys, "color": get_random_color()},
    96. "type":{"value":type, "color": get_random_color()},
    97. "tep_high":{"value":tep_high, "color": get_random_color()},
    98. "tep_low":{"value":tep_low, "color": get_random_color()},
    99. "notice":{"value":notice, "color": get_random_color()},
    100. "love_days":{"value":get_count(), "color": get_random_color()},
    101. "birthday_left":{"value":get_birthday(), "color": get_random_color()},
    102. "words":{"value":get_words(), "color": get_random_color()}}
    103. # 群发消息
    104. for i in range(len(user_id)):
    105. res = wm.send_template(user_id[i], template_id, data)
    106. print(res)

    5、最终效果

     


    Over~

  • 相关阅读:
    DNS解析为什么不生效?DNS解析不生效原因分析
    猿创征文|〖Python 数据库开发实战 - Python与MySQL交互篇⑯〗- 项目实战 - 实现用户管理 - 新增用户功能
    STM32晶振的选择与计算
    89、Redis 的 value 所支持的数据类型(String、List、Set、Zset、Hash)---->Zset 相关命令
    paddle动态图自定义算子(python版)
    PAT Sort with Swap(0, i)
    mybatis-学习笔记
    Flutter开发者开发薪资高吗?前景怎么样
    Prometheus入门与实战
    Flink学习第八天——Flink核心的Sink Operator实战
  • 原文地址:https://blog.csdn.net/qq_40491534/article/details/126558083