We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
我使用服务号搭建了AI的自动回复,但是菜单会失效。我使用了python代码在服务端创建了菜单,但是菜单类型有很多种:‘打开小程序’、‘打开链接’、‘自动回复’。其中自动回复("type": "click",)是通过触发KEY然后自动触发回复,这个在没有接入开发者的时候微信自带支持,但是接入服务器后,就需要服务器自己支持,由于我们cow,服务的信息接受已经被cow占用了,但是cow并没处理"type": "click",消息类型,我希望在配置里面可以添加一个转发到其他服务的功能,我使用其他服务给与微信回复。 以下是我创建菜单,和创建接收服务的整个过程 当我点击联系我们时,请求日志会答应下面的日志 但不会处理
menu.json
{ "button": [ { "name": "合作方", "sub_button": [ { "type": "view", "name": "入围电催&调解方", "url": "https://xxxxx" }, { "type": "view", "name": "入围律师事务所", "url": "https://xxxxxx" }, { "type": "click", "name": "联系我们", "key": "CONTACT_US" } ] }, { "type": "view", "name": "联系客服", "url": "https://work.weixin.qq.com/xxxxx" } ] }
config.py
WECHAT_APPID = 'xxxx' WECHAT_SECRET = 'xxxx' TOKEN = 'xxxx'
responses.py
# responses.py RESPONSES = { "CONTACT_US": "咨询电话:xxx\n邮箱:[email protected]" }
wechat_menu.py
import requests import json from config import WECHAT_APPID, WECHAT_SECRET # 获取 access token def get_access_token(appid, secret): url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}" print("发出请求获取 access token") response = requests.get(url) if response.status_code == 200: data = response.json() return data.get('access_token') else: print(f"获取 access token 失败: {response.text}") return None # 创建菜单 def create_menu(access_token, menu_data): print("进入create_menu") url = f"https://api.weixin.qq.com/cgi-bin/menu/create?access_token={access_token}" response = requests.post(url, data=menu_data.encode('utf-8')) if response.status_code == 200: data = response.json() if data.get('errcode') == 0: print("菜单创建成功") else: print(f"菜单创建失败: {data}") else: print(f"请求失败: {response.text}") print("开始") access_token = get_access_token(WECHAT_APPID, WECHAT_SECRET) print(f"access_token: {access_token}") if access_token: with open('menu.json', 'r', encoding='utf-8') as file: menu_data = file.read() create_menu(access_token, menu_data)
wechat_server.py
from flask import Flask, request, make_response import hashlib import xml.etree.ElementTree as ET import time import argparse from config import TOKEN from responses import RESPONSES app = Flask(__name__) def verify_signature(signature, timestamp, nonce, token): tmp_list = [token, timestamp, nonce] tmp_list.sort() tmp_str = ''.join(tmp_list) hash_str = hashlib.sha1(tmp_str.encode('utf-8')).hexdigest() return hash_str == signature def create_text_response(from_user, to_user, content): response = f""" <xml> <ToUserName><![CDATA[{from_user}]]></ToUserName> <FromUserName><![CDATA[{to_user}]]></FromUserName> <CreateTime>{int(time.time())}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[{content}]]></Content> </xml> """ return response @app.route('/wechat', methods=['GET', 'POST']) def wechat(): if request.method == 'GET': signature = request.args.get('signature') timestamp = request.args.get('timestamp') nonce = request.args.get('nonce') echostr = request.args.get('echostr') if verify_signature(signature, timestamp, nonce, TOKEN): return echostr else: return "Signature verification failed" elif request.method == 'POST': xml_data = request.data xml_tree = ET.fromstring(xml_data) msg_type = xml_tree.find('MsgType').text from_user = xml_tree.find('FromUserName').text to_user = xml_tree.find('ToUserName').text if msg_type == 'event': event = xml_tree.find('Event').text if event == 'CLICK': event_key = xml_tree.find('EventKey').text response_content = RESPONSES.get(event_key, "未找到相应的自动回复内容") response = create_text_response(from_user, to_user, response_content) response = make_response(response) response.content_type = 'application/xml' return response elif msg_type == 'text': content = xml_tree.find('Content').text response_content = f"您发送的是: {content}" response = create_text_response(from_user, to_user, response_content) response = make_response(response) response.content_type = 'application/xml' return response return "success" if __name__ == '__main__': parser = argparse.ArgumentParser(description='WeChat Server') parser.add_argument('--port', type=int, default=80, help='Port to run the server on') args = parser.parse_args() app.run(host='0.0.0.0', port=args.port)
## 安装资源 `pip install -r requirements.txt` ## 创建菜单 `python3 wechat_menu.py` ## 启动关键字触发服务 `python wechat_server.py --port 8888`
No response
The text was updated successfully, but these errors were encountered:
No branches or pull requests
总结
我使用服务号搭建了AI的自动回复,但是菜单会失效。我使用了python代码在服务端创建了菜单,但是菜单类型有很多种:‘打开小程序’、‘打开链接’、‘自动回复’。其中自动回复("type": "click",)是通过触发KEY然后自动触发回复,这个在没有接入开发者的时候微信自带支持,但是接入服务器后,就需要服务器自己支持,由于我们cow,服务的信息接受已经被cow占用了,但是cow并没处理"type": "click",消息类型,我希望在配置里面可以添加一个转发到其他服务的功能,我使用其他服务给与微信回复。
![image](https://private-user-images.githubusercontent.com/61341868/334304096-18fcb315-6cf9-43b9-b21a-4f0f6004b664.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA1NzUwNjIsIm5iZiI6MTcyMDU3NDc2MiwicGF0aCI6Ii82MTM0MTg2OC8zMzQzMDQwOTYtMThmY2IzMTUtNmNmOS00M2I5LWIyMWEtNGYwZjYwMDRiNjY0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzEwVDAxMjYwMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWEwYzgxYjFlN2YxNDY1ZGZhMDY5OTA4OGMwZjYxNTg5N2Y0ZjkyYTU3YWQ5MGEzZjU4MDE2MzE2ZWUxNDE3OTgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.I6U8SKCP3y9dSF3k8bi759TAARxHfXIYB1jv7cyezzQ)
以下是我创建菜单,和创建接收服务的整个过程
当我点击联系我们时,请求日志会答应下面的日志 但不会处理
menu.json
config.py
responses.py
wechat_menu.py
wechat_server.py
举例
No response
动机
No response
The text was updated successfully, but these errors were encountered: