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

login error: wechat network error #3

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
22b8cf4
优化gtp请求代码,请求失败有固定回复
869413421 Dec 7, 2022
e3a8ac6
修改readme
Dec 8, 2022
9b979d8
增加提问上下问,接近官网效果。
Dec 8, 2022
ba9003e
修改README
Dec 8, 2022
f80a211
1.增加会话时长配置功能
Dec 8, 2022
6cd1ccc
修改README
Dec 8, 2022
ef2fa38
支持linux运行
869413421 Dec 8, 2022
1b22bee
修改README
869413421 Dec 8, 2022
529905d
修改README
869413421 Dec 8, 2022
e8298bb
添加自动构建项目镜像的能力
eryajf Dec 9, 2022
c761ab4
添加readme说明
eryajf Dec 9, 2022
04b51e7
readme添加docker运行受命
eryajf Dec 9, 2022
db61c4d
readme添加docker运行说明
eryajf Dec 9, 2022
aa3f9d5
Merge pull request #20 from eryajf/main
869413421 Dec 9, 2022
e3dba40
Update README.md
adsian Dec 9, 2022
652b7ce
修改配置文件
Dec 9, 2022
9247d4e
Merge pull request #27 from adsian/patch-1
869413421 Dec 9, 2022
cfeded5
Merge branch 'cofing' of https://github.com/869413421/wechatbot into …
869413421 Dec 9, 2022
c5b5304
1.增加GTP参数配置
869413421 Dec 9, 2022
eca0451
1.优化日志代码,GPT请求直接返回给用户
869413421 Dec 10, 2022
8af217f
修复群上下文无法清空bug
869413421 Dec 10, 2022
70d4b20
修改配置默认项
869413421 Dec 10, 2022
5092d2a
修改配置默认值
869413421 Dec 10, 2022
efb539f
配置文件模板少个逗号
lafeier888 Dec 10, 2022
ac478c8
增加热登录重试
869413421 Dec 10, 2022
4039fb4
Merge pull request #40 from lafeier888/main
869413421 Dec 10, 2022
1a0adcf
修复配置文件映射的问题
eryajf Dec 11, 2022
df47621
Merge pull request #45 from eryajf/patch-1
869413421 Dec 11, 2022
afec292
1.重构项目代码,增加阅读性
869413421 Dec 11, 2022
0640a26
Merge branch 'main' of https://github.com/869413421/wechatbot
869413421 Dec 11, 2022
1e1e3ca
feat: add build go binary to release
eryajf Dec 11, 2022
a71895f
feat: add build go binary to release
eryajf Dec 11, 2022
0e36038
feat: 调整说明文档
eryajf Dec 11, 2022
4a32233
Merge pull request #48 from eryajf/main
869413421 Dec 11, 2022
6e6fdb1
修改README和GPT错误命名
Dec 12, 2022
6e56c52
使用openwechat dispatcher优化hanlers
Dec 12, 2022
b908859
1.群聊需要@才能清空上下文
869413421 Dec 12, 2022
70df388
Merge pull request #62 from slzheng2017/main
869413421 Dec 12, 2022
1b94ec0
编写Readme文档:
zhang-cuishan Dec 13, 2022
a24dc53
Merge pull request #72 from zhang-cuishan/main
869413421 Dec 13, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# This is a basic workflow to help you get started with Actions

name: build docker image

# Controls when the action will run.
on:
push:
branches:
- main

# Allows you to run this workflow manually from the Actions tab
# 可以手动触发
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
tags:
description: 'Test scenario tags'

jobs:
buildx:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Get current date
id: date
run: echo "::set-output name=today::$(date +'%Y-%m-%d_%H-%M')"

- name: Set up QEMU
uses: docker/setup-qemu-action@v1

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1

- name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
# 所需要的体系结构,可以在 Available platforms 步骤中获取所有的可用架构
platforms: linux/amd64,linux/arm64/v8
# 镜像推送时间
push: ${{ github.event_name != 'pull_request' }}
# 给清单打上多个标签
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/wechatbot:${{ steps.date.outputs.today }}
${{ secrets.DOCKERHUB_USERNAME }}/wechatbot:latest
26 changes: 26 additions & 0 deletions .github/workflows/go-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: build

on:
release:
types: [created] # 表示在创建新的 Release 时触发

jobs:
build-go-binary:
runs-on: ubuntu-latest
strategy:
matrix:
goos: [linux, windows, darwin] # 需要打包的系统
goarch: [amd64, arm64] # 需要打包的架构
exclude: # 排除某些平台和架构
- goarch: arm64
goos: windows
steps:
- uses: actions/checkout@v3
- uses: wangyoucao577/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }} # 一个默认的变量,用来实现往 Release 中添加文件
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: 1.16 # 可以指定编译使用的 Golang 版本
binary_name: "wechatbot" # 可以指定二进制文件的名称
extra_files: README.md config.dev.json # 需要包含的额外文件
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*.so
*.dylib
.idea/
.vscode/
wechatbot
storage.json

# Test binary, built with `go test -c`
Expand All @@ -15,3 +17,4 @@ storage.json

# Dependency directories (remove the comment below to include it)
# vendor/
/config.json
65 changes: 65 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#FROM golang:1.17.10 AS builder
#
## ENV GOPROXY https://goproxy.io
#
#RUN mkdir /app
#ADD . /app/
#WORKDIR /app
#RUN go build -o wechatbot .
#
#FROM centos:centos7
#RUN mkdir /app
#WORKDIR /app
#COPY --from=builder /app/ .
#RUN chmod +x wechatbot && cp config.dev.json config.json && yum -y install vim net-tools telnet wget curl && yum clean all
#
#CMD ./wechatbot

# wechatbot/Dockerfile

# 使用 golang 官方镜像提供 Go 运行环境,并且命名为 buidler 以便后续引用
FROM golang:1.16-alpine as builder

# 启用 Go Modules 并设置 GOPROXY
ENV GO111MODULE on
ENV GOPROXY https://goproxy.cn

# 更新安装源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 安装 git
RUN apk --no-cache add git

# 设置工作目录
WORKDIR /app

# 将当前项目所在目录代码拷贝到镜像中
COPY . .

# 下载依赖
RUN go mod download

# 构建二进制文件,添加来一些额外参数以便可以在 Alpine 中运行它
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o wechatbot

# 下面是第二阶段的镜像构建,和之前保持一致
FROM alpine:latest

# 更新安装源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 安装相关软件
RUN apk update && apk add --no-cache bash supervisor ca-certificates

# 和上个阶段一样设置工作目录
RUN mkdir /app
WORKDIR /app

# 而是从上一个阶段构建的 builder容器中拉取
COPY --from=builder /app/wechatbot .
ADD supervisord.conf /etc/supervisord.conf
ADD config.dev.json /app/config.dev.json
RUN cp config.dev.json config.json

# 通过 Supervisor 管理服务
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: build
build:
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -o wechatbot ./main.go

.PHONY: docker
docker:
docker build . -t wechatbot:latest
170 changes: 154 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,164 @@
# wechatbot
最近chatGPT异常火爆,想到将其接入到个人微信是件比较有趣的事,所以有了这个项目。项目基于[openwechat](https://github.com/eatmoreapple/openwechat)
开发
###目前实现了以下功能
+ 群聊@回复
+ 私聊回复
+ 自动通过回复

# 注册openai
chatGPT注册可以参考[这里](https://juejin.cn/post/7173447848292253704)

# 安装使用

> 最近ChatGPT异常火爆,本项目可以将个人微信化身GPT机器人,
> 项目基于[openwechat](https://github.com/eatmoreapple/openwechat) 开发。

> `友链:`[chatgpt-dingtalk](https://github.com/eryajf/chatgpt-dingtalk) 本项目可以将GPT机器人集成到钉钉群聊中。

[![Release](https://img.shields.io/github/v/release/869413421/wechatbot.svg?style=flat-square)](https://github.com/869413421/wechatbot/releases/tag/v1.1.3)
![Github stars](https://img.shields.io/github/stars/869413421/wechatbot.svg)
![Forks](https://img.shields.io/github/forks/869413421/wechatbot.svg?style=flat-square)

### 目前实现了以下功能

* GPT机器人模型热度可配置
* 提问增加上下文
* 指令清空上下文(指令:根据配置)
* 机器人群聊@回复
* 机器人私聊回复
* 私聊回复前缀设置
* 好友添加自动通过

# 实现机制
目前机器人有两种实现方式
* 逆向功能,扒取官网API,通过抓取cookie获取GPT响应信息,`优点:`效果与官网一致,`缺点:`cookie会过期需要不定时更新。
* 基于openai官网提供的API,`优点`:模型以及各种参数可以自由配置,`缺点:`效果达不到官网智能,且API收费,新账号有18元免费额度。

> 本项目基于第二种方式实现,模型之间具体差异可以参考[官方文档](https://beta.openai.com/docs/models/overview), 详细[参数示例](https://beta.openai.com/examples) 。

# 常见问题
* 如无法登录 login error: write storage.json: bad file descriptor 删除掉storage.json文件重新登录。
* 如无法登录 login error: wechat network error: Get "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage": 301 response missing Location header 一般是微信登录权限问题,先确保PC端能否正常登录。
* 其他无法登录问题,依然尝试删除掉storage.json文件,结束进程(linux一般是kill -9 进程id)之后重启程序,重新扫码登录,(如为docket部署,Supervisord进程管理工具会自动重启程序)。
* ~~机器人无法正常回复,检查ApiKey能否正常使用,控制台日志中有详细错误信息~~ 新版本会机器人会直接输出,因为被问得好烦了。
* linux中二维码无法扫描,缩小命令行功能,让二维码像素尽可能清晰。(无法从代码层面解决)
* 机器人一直答非所问,可能因为上下文累积过多。切换不同问题时,发送指令:启动时配置的`session_clear_token`字段。会清空上下文

# 使用前提

> * ~~目前只支持在windows上运行因为需要弹窗扫码登录微信,后续会支持linux~~ 已支持
> * 有openai账号,并且创建好api_key,注册事项可以参考[此文章](https://juejin.cn/post/7173447848292253704) 。
> * 微信必须实名认证。

# 注意事项

> * 项目仅供娱乐,滥用可能有微信封禁的风险,请勿用于商业用途。
> * 请注意收发敏感信息,本项目不做信息过滤。

# 使用docker运行

你可以使用docker快速运行本项目。

`第一种:基于环境变量运行`

```sh
# 运行项目,环境变量参考下方配置说明
$ docker run -itd --name wechatbot --restart=always \
-e APIKEY=换成你的key \
-e AUTO_PASS=false \
-e SESSION_TIMEOUT=60s \
-e MODEL=text-davinci-003 \
-e MAX_TOKENS=512 \
-e TEMPREATURE=0.9 \
-e REPLY_PREFIX=我是来自机器人回复: \
-e SESSION_CLEAR_TOKEN=下一个问题 \
docker.mirrors.sjtug.sjtu.edu.cn/qingshui869413421/wechatbot:latest

# 查看二维码
$ docker exec -it wechatbot bash
$ tail -f -n 50 /app/run.log
```

运行命令中映射的配置文件参考下边的配置文件说明。

`第二种:基于配置文件挂载运行`

```sh
# 复制配置文件,根据自己实际情况,调整配置里的内容
$ cp config.dev.json config.json # 其中 config.dev.json 从项目的根目录获取

# 运行项目
$ docker run -itd --name wechatbot -v `pwd`/config.json:/app/config.json docker.mirrors.sjtug.sjtu.edu.cn/qingshui869413421/wechatbot:latest

# 查看二维码
$ docker exec -it wechatbot bash
$ tail -f -n 50 /app/run.log
```

其中配置文件参考下边的配置文件说明。

# 快速开始

`第一种:直接下载二进制(适合对编程不了解的同学)`

> 非技术人员请直接下载release中的[压缩包](https://github.com/869413421/wechatbot/releases) ,请根据自己系统以及架构选择合适的压缩包,下载之后直接解压运行。

下载之后,在本地解压,即可看到可执行程序,与配置文件:

```
# windows
1.下载压缩包解压
2.复制文件中config.dev.json更改为config.json
3.将config.json中的api_key替换为自己的
4.双击exe,扫码登录

# linux
$ tar xf wechatbot-v0.0.2-darwin-arm64.tar.gz
$ cd wechatbot-v0.0.2-darwin-arm64
$ cp config.dev.json # 根据情况调整配置文件内容
$ ./wechatbot # 直接运行

# 如果要守护在后台运行
$ nohup ./wechatbot &> run.log &
$ tail -f run.log
```

`第二种:基于源码运行(适合了解go语言编程的同学)`

````
# 获取项目
git clone https://github.com/869413421/wechatbot.git
$ git clone https://github.com/869413421/wechatbot.git

# 进入项目目录
cd wechatbot
$ cd wechatbot

# 复制配置文件
copy config.dev.json config.json
$ copy config.dev.json config.json

# 启动项目
go run main.go
$ go run main.go
````

# 配置文件说明

````
{
"api_key": "your api key",
"auto_pass": true,
"session_timeout": 60,
"max_tokens": 1024,
"model": "text-davinci-003",
"temperature": 1,
"reply_prefix": "来自机器人回复:",
"session_clear_token": "清空会话"
}

api_key:openai api_key
auto_pass:是否自动通过好友添加
session_timeout:会话超时时间,默认60秒,单位秒,在会话时间内所有发送给机器人的信息会作为上下文。
max_tokens: GPT响应字符数,最大2048,默认值512。max_tokens会影响接口响应速度,字符越大响应越慢。
model: GPT选用模型,默认text-davinci-003,具体选项参考官网训练场
temperature: GPT热度,0到1,默认0.9。数字越大创造力越强,但更偏离训练事实,越低越接近训练事实
reply_prefix: 私聊回复前缀
session_clear_token: 会话清空口令,默认`下一个问题`
````

# 使用示例
### 私聊

<img width="300px" src="https://raw.githubusercontent.com/869413421/study/master/static/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20221208153022.jpg"/>

### 群聊@回复

<img width="300px" src="https://raw.githubusercontent.com/869413421/study/master/static/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20221208153015.jpg"/>

启动前需替换config中的api_key
Loading