Skip to content

Commit

Permalink
Merge pull request #21 from tudou2/test_code
Browse files Browse the repository at this point in the history
Test code
  • Loading branch information
congxuma committed Apr 24, 2023
2 parents 58aae5b + c4d7a68 commit 089e7fd
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 97 deletions.
1 change: 1 addition & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def func(_signo, _stack_frame):
if callable(old_handler): # check old_handler
return old_handler(_signo, _stack_frame)
sys.exit(0)

signal.signal(_signo, func)


Expand Down
6 changes: 2 additions & 4 deletions channel/wechatmp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

鉴于个人微信号在服务器上通过itchat登录有封号风险,这里新增了微信公众号channel,提供无风险的服务。

目前支持订阅号和服务号两种类型的公众号。个人主体的微信订阅号由于无法通过微信认证,接口存在限制,目前仅支持最基本的文本交互和语音输入。通过微信认证的订阅号或者服务号可以回复图片和语音。
目前支持订阅号和服务号两种类型的公众号,它们都支持文本交互,语音和图片输入。其中个人主体的微信订阅号由于无法通过微信认证,存在回复时间限制,每天的图片和声音回复次数也有限制。


## 使用方法(订阅号,服务号类似)

Expand All @@ -27,7 +28,6 @@ pip3 install wechatpy
"wechatmp_app_id": "xxxx", # 微信公众平台的appID
"wechatmp_app_secret": "xxxx", # 微信公众平台的appsecret
"wechatmp_aes_key": "", # 微信公众平台的EncodingAESKey,加密模式需要
"single_chat_prefix": [""], # 推荐设置,任意对话都可以触发回复,不添加前缀
"single_chat_reply_prefix": "", # 推荐设置,回复不设置前缀
"plugin_trigger_prefix": "&", # 推荐设置,在手机微信客户端中,$%^等符号与中文连在一起时会自动显示一段较大的间隔,用户体验不好。请不要使用管理员指令前缀"#",这会造成未知问题。
Expand All @@ -44,15 +44,13 @@ sudo iptables-save > /etc/iptables/rules.v4
程序启动并监听端口后,在刚才的“服务器配置”中点击`提交`即可验证你的服务器。
随后在[微信公众平台](https://mp.weixin.qq.com)启用服务器,关闭手动填写规则的自动回复,即可实现ChatGPT的自动回复。


之后需要在公众号开发信息下将本机IP加入到IP白名单。

不然在启用后,发送语音、图片等消息可能会遇到如下报错:
```
'errcode': 40164, 'errmsg': 'invalid ip xx.xx.xx.xx not in whitelist rid
```

需要在公众号开发信息下将IP加入到IP白名单。


## 个人微信公众号的限制
Expand Down
6 changes: 1 addition & 5 deletions channel/wechatmp/active_reply.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import time

import web

from wechatpy import parse_message
from wechatpy.replies import create_reply


from bridge.context import *
from bridge.reply import *
from channel.wechatmp.common import *
Expand All @@ -22,7 +22,6 @@ def GET(self):

def POST(self):
# Make sure to return the instance that first created, @singleton will do that.

try:
args = web.input()
verify_server(args)
Expand All @@ -42,7 +41,6 @@ def POST(self):
wechatmp_msg = WeChatMPMessage(msg, client=channel.client)
from_user = wechatmp_msg.from_user_id
content = wechatmp_msg.content

message_id = wechatmp_msg.msg_id

logger.info(
Expand All @@ -58,7 +56,6 @@ def POST(self):
context = channel._compose_context(wechatmp_msg.ctype, content, isgroup=False, desire_rtype=ReplyType.VOICE, msg=wechatmp_msg)
else:
context = channel._compose_context(wechatmp_msg.ctype, content, isgroup=False, msg=wechatmp_msg)

if context:
# set private openai_api_key
# if from_user is not changed in itchat, this can be placed at chat_channel
Expand All @@ -75,7 +72,6 @@ def POST(self):
return encrypt_func(replyPost.render())
else:
return "success"

else:
logger.info("暂且不处理")
return "success"
Expand Down
2 changes: 1 addition & 1 deletion channel/wechatmp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def verify_server(data):


def subscribe_msg():
trigger_prefix = conf().get("single_chat_prefix", [""])
trigger_prefix = conf().get("single_chat_prefix", [""])[0]
msg = textwrap.dedent(
f"""\
感谢您的关注!
Expand Down
78 changes: 0 additions & 78 deletions channel/wechatmp/passive_reply_message.py

This file was deleted.

7 changes: 1 addition & 6 deletions channel/wechatmp/wechatmp_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import threading
import time
import imghdr

import requests
import web
from wechatpy.crypto import WeChatCrypto
Expand All @@ -14,18 +14,13 @@
from bridge.context import *
from bridge.reply import *
from channel.chat_channel import ChatChannel
from channel.wechatmp.wechatmp_client import WechatMPClient
from channel.wechatmp.common import *
from channel.wechatmp.wechatmp_client import WechatMPClient
from common.log import logger
from common.singleton import singleton
from config import conf
from voice.audio_convert import any_to_mp3

import asyncio
from threading import Thread

import web
# If using SSL, uncomment the following lines, and modify the certificate path.
# from cheroot.server import HTTPServer
# from cheroot.ssl.builtin import BuiltinSSLAdapter
Expand Down
2 changes: 1 addition & 1 deletion channel/wechatmp/wechatmp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ def _request(self, method, url_or_endpoint, **kwargs): # 重载父类方法,
return super()._request(method, url_or_endpoint, **kwargs)
else:
logger.error("[wechatmp] last clear quota time is {}, less than 60s, skip clear quota")
raise e
raise e
2 changes: 1 addition & 1 deletion channel/wechatmp/wechatmp_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ def download_image():

self.from_user_id = msg.source
self.to_user_id = msg.target
self.other_user_id = msg.source
self.other_user_id = msg.source
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ selenium>=3.13.0

Pillow
pre-commit

0 comments on commit 089e7fd

Please sign in to comment.