微信小程序

本文介绍使用Python的itchat和wxpy库实现微信消息自动回复功能,包括手机号验证及天气查询服务。通过调用外部API获取天气信息,并利用正则表达式验证手机号格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#python开发两种形式
#1.itchat
#2.wxpy 对itchat做进一步优化
import itchat
from itchat.content import TEXT
import requests
import re
import json
import requests
api_k = '278ea7d0b4bc4dfe920359a54b294c10'
json_data = {
             "reqType": 0,
             "perception": {
                 "inputText": {
                     "text": "我叫什么?"
                 },

             },
             "userInfo": {
                 "apiKey": api_k,
                 "userId": "hello"
             }
         }

pattern = re.compile(r"1[34578]\d{9}")
#TEXT接受消息类型
@itchat.msg_register(TEXT, isFriendChat=True, isGroupChat=True, isMpChat=True)
def simple_reply(msg):
    print(msg.text)

    phone = msg.text
    result = pattern.match(phone)
    if not result:
      print("手机号格式错误!请重新输入!")
      city = msg.text
      if city in ['郑州','开封','信阳']:
             api = f'http://api.map.baidu.com/telematics/v3/weather?location={city}&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'
             response = requests.get(api)
             # text:普通文本  content:二进制文本
             json_content = response.text

             # 解析json数据

             weather = json.loads(json_content)
             return weather['results'][0]['weather_data'][0]['temperature']

      else:
             url = 'http://openapi.tuling123.com/openapi/api/v2'
             json_data["perception"]["inputText"]["text"] = city
             response = requests.post(url, json=json_data)
             html_content = response.text
             html_content = json.loads(html_content)
             html_content = html_content["results"][0]["values"]["text"]

             return (html_content)

    if result:
      api = f'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={phone}'
      response = requests.get(api)
      other_content = response.text
      other_content = other_content.replace('__GetZoneResult_ = ', '')
      place = other_content.split(",")[-1].split(':')[-1]
      place = place.strip("\n }'")
   # print(place)
      return  place
itchat.auto_login(True)
itchat.run()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值