Winse Blog

走走停停都是风景, 熙熙攘攘都向最好, 忙忙碌碌都为明朝, 何畏之.

微信对接OpenAI

前篇通过sms申请国外手机号注册了OpenAI,在网页上体验了一把ChatGPT。由于需要翻墙,在手机上使用起来并不是很方便,也不能把账号给所有想用的朋友用,范围受限。

最近OpenAI的接口功对接,以及很多文章讲微信对接的文章,我也蹭着这股风把原来的公众号捡起来熟悉熟悉。

使用开源的方案

参考文章:

搞一台国外的服务器(同事通过自己的机器+翻墙也是可以的)

安装Python3.8(OpenAI接口有要求)

我用的服务器是CentOS7的,已经安装了Python3.6,官方yum中最新的也就3.6。使用scl(Software Collections,类似NVM多版本共存)的方式安装:

再回顾:如果还要更多个python版本,安装conda也是一个不错的选择。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
[root@izt4nhcmmx33bjwcsdmf8oz ~]# sudo yum install centos-release-scl

[root@izt4nhcmmx33bjwcsdmf8oz ~]# yum install rh-python38
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * centos-sclo-rh: mirror.vodien.com
 * centos-sclo-sclo: mirror.vodien.com
Resolving Dependencies
--> Running transaction check
---> Package rh-python38.x86_64 0:2.0-4.el7 will be installed
--> Processing Dependency: rh-python38-runtime for package: rh-python38-2.0-4.el7.x86_64
--> Processing Dependency: rh-python38-python for package: rh-python38-2.0-4.el7.x86_64
--> Processing Dependency: rh-python38-python-pip for package: rh-python38-2.0-4.el7.x86_64
--> Processing Dependency: rh-python38-python-setuptools for package: rh-python38-2.0-4.el7.x86_64
--> Running transaction check
---> Package rh-python38-python.x86_64 0:3.8.13-1.el7 will be installed
--> Processing Dependency: rh-python38-python-libs(x86-64) = 3.8.13-1.el7 for package: rh-python38-python-3.8.13-1.el7.x86_64
--> Processing Dependency: libpython3.8.so.rh-python38-1.0()(64bit) for package: rh-python38-python-3.8.13-1.el7.x86_64
---> Package rh-python38-python-pip.noarch 0:19.3.1-3.el7 will be installed
---> Package rh-python38-python-setuptools.noarch 0:41.6.0-7.el7 will be installed
---> Package rh-python38-runtime.x86_64 0:2.0-4.el7 will be installed
--> Running transaction check
---> Package rh-python38-python-libs.x86_64 0:3.8.13-1.el7 will be installed
--> Processing Dependency: rh-python38-python-pip-wheel for package: rh-python38-python-libs-3.8.13-1.el7.x86_64
--> Processing Dependency: rh-python38-python-setuptools-wheel for package: rh-python38-python-libs-3.8.13-1.el7.x86_64
--> Running transaction check
---> Package rh-python38-python-pip-wheel.noarch 0:19.3.1-3.el7 will be installed
---> Package rh-python38-python-setuptools-wheel.noarch 0:41.6.0-7.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================
 Package                                              Arch                    Version                         Repository                       Size
====================================================================================================================================================
Installing:
 rh-python38                                          x86_64                  2.0-4.el7                       centos-sclo-rh                  2.8 k
Installing for dependencies:
 rh-python38-python                                   x86_64                  3.8.13-1.el7                    centos-sclo-rh                   71 k
 rh-python38-python-libs                              x86_64                  3.8.13-1.el7                    centos-sclo-rh                  7.7 M
 rh-python38-python-pip                               noarch                  19.3.1-3.el7                    centos-sclo-rh                  1.6 M
 rh-python38-python-pip-wheel                         noarch                  19.3.1-3.el7                    centos-sclo-rh                  1.0 M
 rh-python38-python-setuptools                        noarch                  41.6.0-7.el7                    centos-sclo-rh                  636 k
 rh-python38-python-setuptools-wheel                  noarch                  41.6.0-7.el7                    centos-sclo-rh                  299 k
 rh-python38-runtime                                  x86_64                  2.0-4.el7                       centos-sclo-rh                  1.1 M

Transaction Summary
====================================================================================================================================================
Install  1 Package (+7 Dependent packages)

Total download size: 12 M
Installed size: 48 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/centos-sclo-rh/packages/rh-python38-2.0-4.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f2ee9d55: NOKEY
Public key for rh-python38-2.0-4.el7.x86_64.rpm is not installed
(1/8): rh-python38-2.0-4.el7.x86_64.rpm                                                                                      | 2.8 kB  00:00:00     
(2/8): rh-python38-python-3.8.13-1.el7.x86_64.rpm                                                                            |  71 kB  00:00:00     
(3/8): rh-python38-python-pip-19.3.1-3.el7.noarch.rpm                                                                        | 1.6 MB  00:00:00     
(4/8): rh-python38-python-pip-wheel-19.3.1-3.el7.noarch.rpm                                                                  | 1.0 MB  00:00:00     
(5/8): rh-python38-python-setuptools-41.6.0-7.el7.noarch.rpm                                                                 | 636 kB  00:00:00     
(6/8): rh-python38-python-libs-3.8.13-1.el7.x86_64.rpm                                                                       | 7.7 MB  00:00:00     
(7/8): rh-python38-python-setuptools-wheel-41.6.0-7.el7.noarch.rpm                                                           | 299 kB  00:00:00     
(8/8): rh-python38-runtime-2.0-4.el7.x86_64.rpm                                                                              | 1.1 MB  00:00:00     
----------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                21 MB/s |  12 MB  00:00:00     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
Importing GPG key 0xF2EE9D55:
 Userid     : "CentOS SoftwareCollections SIG (https://wiki.centos.org/SpecialInterestGroup/SCLo) <security@centos.org>"
 Fingerprint: c4db d535 b1fb ba14 f8ba 64a8 4eb8 4e71 f2ee 9d55
 Package    : centos-release-scl-rh-2-3.el7.centos.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : rh-python38-runtime-2.0-4.el7.x86_64                                                                                             1/8 
  Installing : rh-python38-python-pip-wheel-19.3.1-3.el7.noarch                                                                                 2/8 
  Installing : rh-python38-python-setuptools-wheel-41.6.0-7.el7.noarch                                                                          3/8 
  Installing : rh-python38-python-setuptools-41.6.0-7.el7.noarch                                                                                4/8 
  Installing : rh-python38-python-3.8.13-1.el7.x86_64                                                                                           5/8 
  Installing : rh-python38-python-libs-3.8.13-1.el7.x86_64                                                                                      6/8 
  Installing : rh-python38-python-pip-19.3.1-3.el7.noarch                                                                                       7/8 
  Installing : rh-python38-2.0-4.el7.x86_64                                                                                                     8/8 
  Verifying  : rh-python38-2.0-4.el7.x86_64                                                                                                     1/8 
  Verifying  : rh-python38-python-pip-wheel-19.3.1-3.el7.noarch                                                                                 2/8 
  Verifying  : rh-python38-python-setuptools-wheel-41.6.0-7.el7.noarch                                                                          3/8 
  Verifying  : rh-python38-python-3.8.13-1.el7.x86_64                                                                                           4/8 
  Verifying  : rh-python38-python-pip-19.3.1-3.el7.noarch                                                                                       5/8 
  Verifying  : rh-python38-python-libs-3.8.13-1.el7.x86_64                                                                                      6/8 
  Verifying  : rh-python38-python-setuptools-41.6.0-7.el7.noarch                                                                                7/8 
  Verifying  : rh-python38-runtime-2.0-4.el7.x86_64                                                                                             8/8 

Installed:
  rh-python38.x86_64 0:2.0-4.el7                                                                                                                    

Dependency Installed:
  rh-python38-python.x86_64 0:3.8.13-1.el7                              rh-python38-python-libs.x86_64 0:3.8.13-1.el7                              
  rh-python38-python-pip.noarch 0:19.3.1-3.el7                          rh-python38-python-pip-wheel.noarch 0:19.3.1-3.el7                         
  rh-python38-python-setuptools.noarch 0:41.6.0-7.el7                   rh-python38-python-setuptools-wheel.noarch 0:41.6.0-7.el7                  
  rh-python38-runtime.x86_64 0:2.0-4.el7                               

Complete!
[root@izt4nhcmmx33bjwcsdmf8oz ~]# 

检测一下,查看Python版本:

1
2
3
4
5
6
7
[root@izt4nhcmmx33bjwcsdmf8oz ~]# scl enable rh-python38 bash

[root@izt4nhcmmx33bjwcsdmf8oz ~]# python --version
Python 3.8.13
[root@izt4nhcmmx33bjwcsdmf8oz ~]# pip --version 
pip 19.3.1 from /opt/rh/rh-python38/root/usr/lib/python3.8/site-packages/pip (python 3.8)
[root@izt4nhcmmx33bjwcsdmf8oz ~]# 

下载代码:

1
[root@izt4nhcmmx33bjwcsdmf8oz ~]# git clone https://github.com/zhayujie/bot-on-anything

对接个人微信

1 安装依赖

参考 https://github.com/zhayujie/chatgpt-on-wechat 的说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
## [root@izt4nhcmmx33bjwcsdmf8oz Python-3.8.1]# yum install libffi-devel

[root@izt4nhcmmx33bjwcsdmf8oz ~]# pip3 install itchat-uos==1.5.0.dev0
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting itchat-uos==1.5.0.dev0
  Downloading http://mirrors.aliyun.com/pypi/packages/31/2b/0be7e46195dc3c461518b046a6fb9c34c97cd2fd44847b840a0011686575/itchat_uos-1.5.0.dev0-py3-none-any.whl (52kB)
     |████████████████████████████████| 61kB 1.9MB/s 
Collecting requests
  Downloading http://mirrors.aliyun.com/pypi/packages/d2/f4/274d1dbe96b41cf4e0efb70cbced278ffd61b5c7bb70338b62af94ccb25b/requests-2.28.2-py3-none-any.whl (62kB)
     |████████████████████████████████| 71kB 2.0MB/s 
Collecting pyqrcode
  Downloading http://mirrors.aliyun.com/pypi/packages/37/61/f07226075c347897937d4086ef8e55f0a62ae535e28069884ac68d979316/PyQRCode-1.2.1.tar.gz
Collecting pypng
  Downloading http://mirrors.aliyun.com/pypi/packages/3e/b9/3766cc361d93edb2ce81e2e1f87dd98f314d7d513877a342d31b30741680/pypng-0.20220715.0-py3-none-any.whl (58kB)
     |████████████████████████████████| 61kB 1.8MB/s 
Collecting certifi>=2017.4.17
  Downloading http://mirrors.aliyun.com/pypi/packages/71/4c/3db2b8021bd6f2f0ceb0e088d6b2d49147671f25832fb17970e9b583d742/certifi-2022.12.7-py3-none-any.whl (155kB)
     |████████████████████████████████| 163kB 1.7MB/s 
Collecting idna<4,>=2.5
  Downloading http://mirrors.aliyun.com/pypi/packages/fc/34/3030de6f1370931b9dbb4dad48f6ab1015ab1d32447850b9fc94e60097be/idna-3.4-py3-none-any.whl (61kB)
     |████████████████████████████████| 71kB 1.9MB/s 
Collecting urllib3<1.27,>=1.21.1
  Downloading http://mirrors.aliyun.com/pypi/packages/fe/ca/466766e20b767ddb9b951202542310cba37ea5f2d792dae7589f1741af58/urllib3-1.26.14-py2.py3-none-any.whl (140kB)
     |████████████████████████████████| 143kB 1.3MB/s 
Collecting charset-normalizer<4,>=2
  Downloading http://mirrors.aliyun.com/pypi/packages/20/a2/16b2cbf5f73bdd10624b94647b85c008ba25059792a5c7b4fdb8358bceeb/charset_normalizer-3.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (195kB)
     |████████████████████████████████| 204kB 1.5MB/s 
Installing collected packages: certifi, idna, urllib3, charset-normalizer, requests, pyqrcode, pypng, itchat-uos
    Running setup.py install for pyqrcode ... done
Successfully installed certifi-2022.12.7 charset-normalizer-3.0.1 idna-3.4 itchat-uos-1.5.0.dev0 pypng-0.20220715.0 pyqrcode-1.2.1 requests-2.28.2 urllib3-1.26.14

[root@izt4nhcmmx33bjwcsdmf8oz ~]# pip3 install --upgrade openai
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting openai
  Downloading http://mirrors.aliyun.com/pypi/packages/2f/15/b29ed5ad2eed1f49b8b3e2067fbbbb63cfbfc496ca2a27b772bcf0907ad8/openai-0.27.0-py3-none-any.whl (70kB)
     |████████████████████████████████| 71kB 1.7MB/s 
Collecting tqdm
  Downloading http://mirrors.aliyun.com/pypi/packages/e6/02/a2cff6306177ae6bc73bc0665065de51dfb3b9db7373e122e2735faf0d97/tqdm-4.65.0-py3-none-any.whl (77kB)
     |████████████████████████████████| 81kB 1.9MB/s 
Collecting aiohttp
  Downloading http://mirrors.aliyun.com/pypi/packages/d2/e5/cef5eeb11d7e8bac830b3bee1c8311b19bf8e8a1c45fe14b876c70adcd06/aiohttp-3.8.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0MB)
     |████████████████████████████████| 1.0MB 1.5MB/s 
Requirement already satisfied, skipping upgrade: requests>=2.20 in /opt/rh/rh-python38/root/usr/local/lib/python3.8/site-packages (from openai) (2.28.2)
Collecting yarl<2.0,>=1.0
  Downloading http://mirrors.aliyun.com/pypi/packages/b3/0d/0ba1f2022b9a36ae670c1f3c579ed08d0958398cb6beaf4687e606ad33d4/yarl-1.8.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (262kB)
     |████████████████████████████████| 266kB 1.7MB/s 
Collecting aiosignal>=1.1.2
  Downloading http://mirrors.aliyun.com/pypi/packages/76/ac/a7305707cb852b7e16ff80eaf5692309bde30e2b1100a1fcacdc8f731d97/aiosignal-1.3.1-py3-none-any.whl
Collecting attrs>=17.3.0
  Downloading http://mirrors.aliyun.com/pypi/packages/fb/6e/6f83bf616d2becdf333a1640f1d463fef3150e2e926b7010cb0f81c95e88/attrs-22.2.0-py3-none-any.whl (60kB)
     |████████████████████████████████| 61kB 1.7MB/s 
Requirement already satisfied, skipping upgrade: charset-normalizer<4.0,>=2.0 in /opt/rh/rh-python38/root/usr/local/lib64/python3.8/site-packages (from aiohttp->openai) (3.0.1)
Collecting frozenlist>=1.1.1
  Downloading http://mirrors.aliyun.com/pypi/packages/ec/ab/a440db757401a1e8863c9abb374a77cb2884eda74ffbf555dedcf1fbe7f6/frozenlist-1.3.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (161kB)
     |████████████████████████████████| 163kB 1.8MB/s 
Collecting multidict<7.0,>=4.5
  Downloading http://mirrors.aliyun.com/pypi/packages/fe/0c/8469202f8f4b0e65816f91c3febc4bda7316c995b59ecdf3b15c574f7a24/multidict-6.0.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (121kB)
     |████████████████████████████████| 122kB 1.9MB/s 
Collecting async-timeout<5.0,>=4.0.0a3
  Downloading http://mirrors.aliyun.com/pypi/packages/d6/c1/8991e7c5385b897b8c020cdaad718c5b087a6626d1d11a23e1ea87e325a7/async_timeout-4.0.2-py3-none-any.whl
Requirement already satisfied, skipping upgrade: idna<4,>=2.5 in /opt/rh/rh-python38/root/usr/local/lib/python3.8/site-packages (from requests>=2.20->openai) (3.4)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /opt/rh/rh-python38/root/usr/local/lib/python3.8/site-packages (from requests>=2.20->openai) (2022.12.7)
Requirement already satisfied, skipping upgrade: urllib3<1.27,>=1.21.1 in /opt/rh/rh-python38/root/usr/local/lib/python3.8/site-packages (from requests>=2.20->openai) (1.26.14)
Installing collected packages: tqdm, multidict, yarl, frozenlist, aiosignal, attrs, async-timeout, aiohttp, openai
Successfully installed aiohttp-3.8.4 aiosignal-1.3.1 async-timeout-4.0.2 attrs-22.2.0 frozenlist-1.3.3 multidict-6.0.4 openai-0.27.0 tqdm-4.65.0 yarl-1.8.2
[root@izt4nhcmmx33bjwcsdmf8oz ~]# 

2 修改配置用个人微信方式使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# cp config-template.json config.json

[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# cat config.json 
{
  "model": {
    "type" : "chatgpt",
    "openai": {
      "api_key": "sk-xxx",
      "proxy": "",
      "conversation_max_tokens": 1000,
      "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。"
    }
  },

  "channel": {
    "type": "wechat",
    "single_chat_prefix": ["bot", "@bot"],
    "single_chat_reply_prefix": "[bot] ",
    "group_chat_prefix": ["@bot"],
    "group_name_white_list": ["ChatGPT测试群"],
    "image_create_prefix": ["画", "看", "找一张"],

    "terminal": {
    },

    "wechat": {
    },

配置解释说明:

1
2
3
4
5
  "single_chat_prefix": ["bot", "@bot"], # 私聊时文本需要包含该前缀才能触发机器人回复
  "single_chat_reply_prefix": "[bot] ", # 私聊时自动回复的前缀,用于区分真人
  "group_chat_prefix": ["@bot"], # 群聊时包含该前缀则会触发机器人回复
  "group_name_white_list": ["ChatGPT测试群", "ChatGPT测试群2"], # 开启自动回复的群名称列表
  "image_create_prefix": ["画", "看", "找"], # 开启图片回复的前缀

可以把 group_chat_prefix 改成你要使用的微信号的名称,这样人家@你,就能触发机器人了。

3 启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# cat startup.sh 
#!/bin/bash

scl enable rh-python38 - << \EOF

mv nohup.out{,$(date +'%Y%m%d%H%M')}
touch nohup.out
nohup python3 app.py & tail -f nohup.out

EOF

[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# 

[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# sh startup.sh 

启动后,会输出一个二维码,使用微信扫描登录。然后建群 ChatGPT测试群 就可以和ChatGPT聊天了。

对接微信公众号

1 做域名外网映射

微信公众号要求443或者80端口,首先用 花生壳 做一个映射(还不要求外网ip的443端口)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[root@izt4nhcmmx33bjwcsdmf8oz ~]# wget "https://down.oray.com/hsk/linux/phddns_5.2.0_amd64.rpm" -O phddns_5.2.0_amd64.rpm
--2023-03-16 12:59:06--  https://down.oray.com/hsk/linux/phddns_5.2.0_amd64.rpm

Resolving down.oray.com (down.oray.com)... 163.181.42.225, 163.181.42.228, 163.181.42.223, ...
Connecting to down.oray.com (down.oray.com)|163.181.42.225|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 295240 (288K) [application/x-redhat-package-manager]
Saving to: ‘phddns_5.2.0_amd64.rpm’

100%[==========================================================================================================>] 295,240     1.25MB/s   in 0.2s   

2023-03-16 12:59:07 (1.25 MB/s) - ‘phddns_5.2.0_amd64.rpm’ saved [295240/295240]

[root@izt4nhcmmx33bjwcsdmf8oz ~]# 

[root@izt4nhcmmx33bjwcsdmf8oz ~]# rpm -ivh phddns_5.2.0_amd64.rpm       
Preparing...                          ################################# [100%]

+----------------Phtunnel start install--------------+

Updating / installing...
   1:phddns-5.2.0-1                   ################################# [100%]
Binary file /proc/1/cmdline matches
Created symlink from /etc/systemd/system/multi-user.target.wants/phtunnel.service to /usr/lib/systemd/system/phtunnel.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/phddns_mini_httpd.service to /usr/lib/systemd/system/phddns_mini_httpd.service.

+-----------Phtunnel Service Install Success---------+


+--------------------------------------------------------+
|                Oray Phtunnel Linux 5.2.0               |
+--------------------------------------------------------+
|     SN: orayc1xxxxx   Default password: admin     |
+--------------------------------------------------------+
| Usage: phddns(start|status|stop|restart|reset|version) |
+--------------------------------------------------------+
|       Remote Management Address http://b.oray.com      |
+--------------------------------------------------------+

[root@izt4nhcmmx33bjwcsdmf8oz ~]# 

# 卸载:yum remove phddns
# phddns start(启动)| status(状态)| stop(停止)|restart(重启)| reset(重置)|enable(开机自启动)|disable(关闭开机自启动)|version(版本)

用上面的SN号和密码登录,在linux上会以一个设备的方式,然后绑定到我们的账号,

然后再到自己的账号设置 https://console.hsk.oray.com/forward 内网穿透,配置一个https映射的。

2 修改配置连接公众号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# cat config.json 
{
  "model": {
    "type" : "chatgpt",
    "openai": {
      "api_key": "sk-xxxxxx",
      "proxy": "",
      "conversation_max_tokens": 1000,
      "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。"
    }
  },

  "channel": {
    "type": "wechat_mp",
    "single_chat_prefix": ["bot", "@bot"],
    "single_chat_reply_prefix": "[ChatGPT] ",
    "group_chat_prefix": ["@bot"],
    "group_name_white_list": ["ChatGPT测试群", "ChatGPT哇哇哇", "刘兰家", "ChatGPT"],
    "image_create_prefix": ["画", "看", "找一张"],

    "terminal": {
    },

    "wechat": {
    },

    "wechat_mp": {
      "token": "xxxxxx",
      "port": "3000"
    },
  

3 安装依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# scl enable rh-python38 bash 
[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# pip3 install werobot
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting werobot
  Downloading http://mirrors.aliyun.com/pypi/packages/84/98/6a16bc6c9b3c216efd28babe06f1891ff1fe3f7f96dfb88516545a8c9b30/WeRoBot-1.13.1-py2.py3-none-any.whl (44kB)
     |████████████████████████████████| 51kB 2.0MB/s 
Collecting bottle
  Downloading http://mirrors.aliyun.com/pypi/packages/bb/1f/5977ea88c6a3df6199db97d320e5da816d415d1eb75a987a1f6823d5cc9d/bottle-0.12.25-py3-none-any.whl (90kB)
     |████████████████████████████████| 92kB 1.8MB/s 
Requirement already satisfied: requests in /opt/rh/rh-python38/root/usr/local/lib/python3.8/site-packages (from werobot) (2.28.2)
Collecting xmltodict
  Downloading http://mirrors.aliyun.com/pypi/packages/94/db/fd0326e331726f07ff7f40675cd86aa804bfd2e5016c727fa761c934990e/xmltodict-0.13.0-py2.py3-none-any.whl
Requirement already satisfied: certifi>=2017.4.17 in /opt/rh/rh-python38/root/usr/local/lib/python3.8/site-packages (from requests->werobot) (2022.12.7)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/rh/rh-python38/root/usr/local/lib/python3.8/site-packages (from requests->werobot) (1.26.14)
Requirement already satisfied: charset-normalizer<4,>=2 in /opt/rh/rh-python38/root/usr/local/lib64/python3.8/site-packages (from requests->werobot) (3.0.1)
Requirement already satisfied: idna<4,>=2.5 in /opt/rh/rh-python38/root/usr/local/lib/python3.8/site-packages (from requests->werobot) (3.4)
Installing collected packages: bottle, xmltodict, werobot
Successfully installed bottle-0.12.25 werobot-1.13.1 xmltodict-0.13.0
[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# 

再下载一个敏感字过滤文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# wget https://raw.githubusercontent.com/qloog/sensitive_words/master/keywords
--2023-03-16 13:52:30--  https://raw.githubusercontent.com/qloog/sensitive_words/master/keywords
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.110.133, 185.199.109.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 289615 (283K) [text/plain]
Saving to: ‘keywords’

100%[==========================================================================================================>] 289,615     --.-K/s   in 0.009s  

2023-03-16 13:52:31 (32.0 MB/s) - ‘keywords’ saved [289615/289615]

[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# mv keywords  sensitive_words.txt
[root@izt4nhcmmx33bjwcsdmf8oz bot-on-anything]# 

配置好后,先启动服务,再修改微信公众号接口地址。

启动还是用上面的脚本即可:

1
2
Bottle v0.12.25 server starting up (using AutoServer())...
Listening on http://0.0.0.0:3000/

4 修改微信公众号的接口地址

设置与开发 - 基本配置服务器配置服务器地址令牌TOKEN 与 config.json 里面的一致。

然后就可以用微信公众号与ChatGPT聊天了。

自己码

1 对接

参考:

在微信公众号开启接口自动回复功能,在基本配置-服务器配置指定地址和令牌。

服务器地址(URL) https://xxx.goho.co/

地址的端口要是80的,我们可以通过 花生壳内网映射外网的软件 来配置一个https,指定到本地机器启动的服务。

2 socks proxy:

3 pkg 打包nodejs程序

–END

Comments