Skip to content

Commit

Permalink
⚗️ 项目初始完成
Browse files Browse the repository at this point in the history
  • Loading branch information
eryajf committed Dec 9, 2022
1 parent 71f7d76 commit c0e78b3
Show file tree
Hide file tree
Showing 12 changed files with 619 additions and 0 deletions.
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 }}/chatgpt-dingtalk:${{ steps.date.outputs.today }}
${{ secrets.DOCKERHUB_USERNAME }}/chatgpt-dingtalk:latest
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
.idea/
.vscode/

chatgpt-dingtalk

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
config.json
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.17.10 AS builder

# ENV GOPROXY https://goproxy.io

RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN go build -o chatgpt-dingtalk .

FROM centos:centos7
RUN mkdir /app
WORKDIR /app
COPY --from=builder /app/ .
RUN chmod +x chatgpt-dingtalk && cp config.dev.json config.json && yum -y install vim net-tools telnet wget curl && yum clean all

CMD ./chatgpt-dingtalk
135 changes: 135 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<div align="center">
<h1>Chatgpt Dingtalk</h1>

[![Auth](https://img.shields.io/badge/Auth-eryajf-ff69b4)](https://github.com/eryajf)
[![Go Version](https://img.shields.io/github/go-mod/go-version/eryajf-world/chatgpt-dingtalk)](https://github.com/eryajf/chatgpt-dingtalk)
[![Gin Version](https://img.shields.io/badge/Gin-1.6.3-brightgreen)](https://github.com/eryajf/chatgpt-dingtalk)
[![Gorm Version](https://img.shields.io/badge/Gorm-1.20.12-brightgreen)](https://github.com/eryajf/chatgpt-dingtalk)
[![GitHub Issues](https://img.shields.io/github/issues/eryajf/chatgpt-dingtalk.svg)](https://github.com/eryajf/chatgpt-dingtalk/issues)
[![GitHub Pull Requests](https://img.shields.io/github/issues-pr/eryajf/chatgpt-dingtalk)](https://github.com/eryajf/chatgpt-dingtalk/pulls)
[![GitHub Pull Requests](https://img.shields.io/github/stars/eryajf/chatgpt-dingtalk)](https://github.com/eryajf/chatgpt-dingtalk/stargazers)
[![HitCount](https://views.whatilearened.today/views/github/eryajf/chatgpt-dingtalk.svg)](https://github.com/eryajf/chatgpt-dingtalk)
[![GitHub license](https://img.shields.io/github/license/eryajf/chatgpt-dingtalk)](https://github.com/eryajf/chatgpt-dingtalk/blob/main/LICENSE)

<p> 🌉 在钉钉群聊中添加chatGPT机器人 🌉</p>

<img src="https://camo.githubusercontent.com/82291b0fe831bfc6781e07fc5090cbd0a8b912bb8b8d4fec0696c881834f81ac/68747470733a2f2f70726f626f742e6d656469612f394575424971676170492e676966" width="800" height="3">
</div><br>



## 前言
> 最近chatGPT异常火爆,本项目可以将GPT机器人集成到钉钉群聊中。
> `注意:`这个项目借鉴了[wechatbot](https://github.com/869413421/wechatbot.git),wechatbot是一个能够集成到个人微信的GPT机器人。
### 功能简介
* 支持在钉钉群聊中添加机器人,通过@机器人进行聊天交互。
* 提问增加上下文(可能不太理想),更接近官网效果。

## 使用前提
> * 有openai账号,并且创建好api_key,注册相关事项可以参考[此文章](https://juejin.cn/post/7173447848292253704) 。访问[这里](https://beta.openai.com/account/api-keys),申请个人秘钥。
> * 在钉钉开发者后台创建机器人,配置应用程序回调。
## 使用教程

### 第一步,先创建机器人

创建步骤参考文档:[企业内部开发机器人](https://open.dingtalk.com/document/robots/enterprise-created-chatbot),或者根据如下步骤进行配置。

1. 创建机器人。
![](https://t.eryajf.net/imgs/2022/12/39583c58f1954374.png)

步骤比较简单,这里就不赘述了。

2. 配置机器人回调接口。
![](https://t.eryajf.net/imgs/2022/12/0227aea3e0688f00.png)

创建完毕之后,点击机器人开发管理,然后配置将要部署的服务所在服务器的出口IP,以及将要给服务配置的域名。

3. 发布机器人。
![](https://t.eryajf.net/imgs/2022/12/30cd2374c1ce3788.png)

点击版本管理与发布,然后点击上线,这个时候就能在钉钉的群里中添加这个机器人了。

4. 群聊添加机器人。

![](https://t.eryajf.net/imgs/2022/12/a5ee1425a9286fbf.png)

### 第二步,部署应用

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

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

```sh
# 运行项目
$ docker run -itd --name chatgpt -e ApiKey=xxxx -e SessionTimeout=60s --restart=always docker.mirrors.sjtug.sjtu.edu.cn/eryajf/chatgpt-dingtalk:latest
```

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

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

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

# 运行项目
docker run -itd --name chatgpt -v ./config.json:/app/config.json --restart=always docker.mirrors.sjtug.sjtu.edu.cn/eryajf/chatgpt-dingtalk:latest
```

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

部署完成之后,通过Nginx代理本服务:

```nginx
server {
listen 80;
server_name chat.eryajf.net;
client_header_timeout 120s;
client_body_timeout 120s;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass https://localhost:8090;
}
}
```

部署完成之后,就可以在群里艾特机器人进行体验了。

效果如下:

![](https://t.eryajf.net/imgs/2022/12/2ad746f6fce04369.png)

## 本地开发

````sh
# 获取项目
$ git clone https://github.com/eryajf/chatgpt-dingtalk.git

# 进入项目目录
$ cd chatgpt-dingtalk

# 复制配置文件,根据个人实际情况进行配置
$ cp config.dev.json config.json

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

## 配置文件说明
````json
{
"api_key": "xxxxxxxxx", // openai api_key
"session_timeout": 60 // 会话超时时间,默认60秒,在会话时间内所有发送给机器人的信息会作为上下文
}
````

## 感谢

- [wechatbot](https://github.com/869413421/wechatbot.git)
4 changes: 4 additions & 0 deletions config-dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"api_key": "xxxxxxxxx",
"session_timeout": 60
}
58 changes: 58 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package config

import (
"encoding/json"
"log"
"os"
"sync"
"time"
)

// Configuration 项目配置
type Configuration struct {
// gtp apikey
ApiKey string `json:"api_key"`
// 会话超时时间
SessionTimeout time.Duration `json:"session_timeout"`
}

var config *Configuration
var once sync.Once

// LoadConfig 加载配置
func LoadConfig() *Configuration {
once.Do(func() {
// 从文件中读取
config = &Configuration{
SessionTimeout: 1,
}
f, err := os.Open("config.json")
if err != nil {
log.Fatalf("open config err: %v", err)
return
}
defer f.Close()
encoder := json.NewDecoder(f)
err = encoder.Decode(config)
if err != nil {
log.Fatalf("decode config err: %v", err)
return
}

// 如果环境变量有配置,读取环境变量
ApiKey := os.Getenv("ApiKey")
SessionTimeout := os.Getenv("SessionTimeout")
if ApiKey != "" {
config.ApiKey = ApiKey
}
if SessionTimeout != "" {
duration, err := time.ParseDuration(SessionTimeout)
if err != nil {
log.Fatalf("config decode session timeout err: %v ,get is %v", err, SessionTimeout)
return
}
config.SessionTimeout = duration
}
})
return config
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/eryajf/chatgpt-dingtalk

go 1.17

require (
github.com/eatmoreapple/openwechat v1.2.3
github.com/patrickmn/go-cache v2.1.0+incompatible
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/eatmoreapple/openwechat v1.2.3 h1:8AO+nvXwHVTM/7Gk7y6IZ2/hjnILTLQztWmJnPhPB+k=
github.com/eatmoreapple/openwechat v1.2.3/go.mod h1:61HOzTyvLobGdgWhL68jfGNwTJEv0mhQ1miCXQrvWU8=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
Loading

0 comments on commit c0e78b3

Please sign in to comment.