Skip to content
New issue

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

fix itchat bug on mac or not mac. #166

Merged
merged 14 commits into from Mar 21, 2023
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -202,6 +202,11 @@ pip3 install --upgrade openai
```
注:`itchat-uos`使用指定版本1.5.0.dev0,`openai`使用最新版本,需高于0.27.0。

**修复 itchat-uos bug**
···
bash fix-itchat.sh
···


**配置项说明:**

Expand Down Expand Up @@ -475,3 +480,8 @@ pip3 install PyJWT flask
本地运行:`python3 app.py`运行后访问 `http://127.0.0.1:80`

服务器运行:部署后访问 `http://公网域名或IP:端口`

**允许无密码访问**
```
bash ./allow-http-nopassword.sh
```
16 changes: 16 additions & 0 deletions allow-http-nopassword.sh
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -x

# @see https://stackoverflow.com/questions/30003570/how-to-use-gnu-sed-on-mac-os-10-10-brew-install-default-names-no-longer-su
# @see https://www.cnblogs.com/fnlingnzb-learner/p/10657285.html
cmd=sed
if [ "$(uname)" == "Darwin" ];then
brew install gnu-sed
cmd=gsed
fi

echo "current sed command is: $cmd"

echo "allow http nopasword"
$cmd -i "s/\"http_auth_password\": \".*\"/\"http_auth_password\": \"\"/" config.json
6 changes: 6 additions & 0 deletions app.py
Expand Up @@ -3,12 +3,18 @@
import config
from channel import channel_factory
from common import log
import os

if __name__ == '__main__':
try:
# load config
config.load_config()

proxy = config.conf().get("model").get("openai").get("proxy")
if proxy:
os.environ['http_proxy'] = proxy
os.environ['https_proxy'] = proxy
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里设置成全局代理会不会影响 wx 的连接

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不影响。

不加的时候。直接会导致无法请求 openai。

image


model_type = config.conf().get("model").get("type")
channel_type = config.conf().get("channel").get("type")

Expand Down
2 changes: 1 addition & 1 deletion channel/channel_factory.py
Expand Up @@ -46,4 +46,4 @@ def create_channel(channel_type):
return HttpChannel()

else:
raise RuntimeError
raise RuntimeError("unknown channel_type in config.json: " + channel_type)
3 changes: 3 additions & 0 deletions channel/http/auth.py
Expand Up @@ -83,6 +83,9 @@ def identify(request):
:return: list
"""
try:
authPassword = channel_conf(const.HTTP).get('http_auth_password')
if (not authPassword):
return True
if (request is None):
return False
authorization = request.cookies.get('Authorization')
Expand Down
10 changes: 9 additions & 1 deletion channel/wechat/wechat_channel.py
Expand Up @@ -40,11 +40,19 @@ def __init__(self):

def startup(self):
# login by scan QRCode
itchat.auto_login(enableCmdQR=2, hotReload=True)
if (channel_conf_val(const.WECHAT, 'receive_qrcode_api')):
itchat.auto_login(enableCmdQR=2, hotReload=True, qrCallback=self.login)
else:
itchat.auto_login(enableCmdQR=2, hotReload=True)

# start message listener
itchat.run()

def login(self, uuid=None, status='0', qrcode=None):
print('uuid:', uuid)
print('status:', status)
# 请将链接转发到外部接口,并在外部自行通过二维码生成库将链接转换为二维码后展示,例如:将下方的 qrcode_link 通过草料二维码进行处理后,再通过手机端扫码登录微信小号
print('qrcode_link:', 'https://login.weixin.qq.com/l/'+uuid)

def handle(self, msg):
logger.debug("[WX]receive msg: " + json.dumps(msg, ensure_ascii=False))
Expand Down
3 changes: 2 additions & 1 deletion config-template.json
Expand Up @@ -13,7 +13,7 @@
"cookie": "YOUR COOKIE"
},
"bing":{
"cookies":[]
"cookies":[]
}
},
"channel": {
Expand All @@ -28,6 +28,7 @@
},

"wechat": {
"receive_qrcode_api": ""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个参数正常应该填什么值

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个 api 地址。例如 http://example.com/api/save-qrcode

然后网页端的其他页面再去读取,渲染成 二维码。
登录完成后,再去删除接收到的这个值。

},

"wechat_mp": {
Expand Down
39 changes: 39 additions & 0 deletions fix-itchat.sh
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

set -x

# @see https://stackoverflow.com/questions/30003570/how-to-use-gnu-sed-on-mac-os-10-10-brew-install-default-names-no-longer-su
# @see https://www.cnblogs.com/fnlingnzb-learner/p/10657285.html
cmd=sed
if [ "$(uname)" == "Darwin" ];then
brew install gnu-sed
cmd=gsed
fi

echo "current sed command is: $cmd"

pack_dir="$(pip3 show itchat-uos | grep "Location" | awk '{print $2}')"
file_name="${pack_dir}/itchat/components/login.py"

sleep15Code="time.sleep(15)"

cat $file_name | grep $sleep15Code

if [ "$?" != "0" ];then
echo "fix $sleep15Code"
$cmd -i "/while not isLoggedIn/i\ $sleep15Code" $file_name
else
echo "already fix $sleep15Code"
fi

sleep3Code="time.sleep(3)"

cat $file_name | grep $sleep3Code

if [ "$?" != "0" ];then
echo "fix $sleep3Code"
$cmd -i "s/elif status != '408'/elif status in ['408', '400']/" $file_name
$cmd -i "/if isLoggedIn:/i\ time.sleep(3)" $file_name
else
echo "already fix $sleep3Code"
fi