Feishu connects to ChatGPT to realize the function of intelligent Q&A assistant and provide efficient answering service

foreword

Create a chatGPT robot in Feishu and have a conversation. In the following steps, the Git clone project is used. Git needs to be installed in advance. The cloned project is a Go language project, so the Go language environment needs to be installed in advance.

environment list

  • Git

  • Go1.20

1. Feishu Settings

When registering for Feishu for the first time, we can create a personal account

image-20230407180923798

After entering, we create a Feishu企业自建项目

image-20230407181129953

Then set the robot name and description, select the icon and color below

image-20230406135537698

After the creation is successful, add the ability of the robot

image-20230406141237807

2. Clone the feishu-chatgpt project

The feishu-chatgpt project integrates the function of calling chatgpt from Feishu. Here, the windwos system is used to clone the project using git in powershell, select the directory, and clone the project

git clone https://github.com/Leizhenpeng/feishu-chatgpt.git

image-20230406131909769

then entercode目录

cd feishu-chatgpt/code

Modify the configuration file name

mv config.example.yaml config.yaml

3. Configure the config.yaml file

In the code folder of the project directory we cloned above, you can see config.yamlthe configuration file

image-20230406171959101

APP IDOpen the configuration file, we can see that the first two parameters correspond to the sum of the project we created on FeishuApp Secret

image-20230406172104162

Open Feishu and copy the corresponding parameters

image-20230406173541210

Fill in these two parameters correspondingly to the configuration file

image-20230406173650789

Then there are two more parameters below: APP_ENCRYPT_KEYandAPP_VERIFICATION_TOKEN

image-20230406173826823

Also in Feishu, click 事件与订阅, and then we can see

image-20230406173948233

The first value is not enabled, we can click refresh

image-20230406174034802

Then we fill in the two values ​​into the configuration file respectively, remember to save

image-20230406174227254

After filling in the parameters of Feishu, the next step is to set the parameters of openAI in the configuration file. We need to obtain the KEY of openAI. You can go to the official website of openAI to obtain it with your own account, or there are some free websites for testing, such as https://freeopenai.xyz / This website, we can get a key, after getting it, we fill in the key value into the configuration file, remember to save the file

image-20230406180330098
Open the configuration file, we can see that the first two parameters correspond to the APP ID and App Secret of the project we created on Feishu

image-20230406172104162
Finally, the device needs to use a proxy. Configure the proxy in the last line. If you do not configure it, you will not be able to access the chatgpt interface, and you will not get the answer returned by chatGPT.

image-20230425173633342

4. Run the feishu-chatgpt project

The first run will download the relevant required packages. In order to prevent the download from being successful, first set the proxy in go and execute the following command切换国内代理

go env -w GOPROXY=https://goproxy.cn,direct

Then 运行main.go文件, start the project,

go run main.go

Pay attention to the path above the picture. It must be operated under the code folder of the feishu-chatgpt project. After starting, we can see the port number. The port number is:9000

image-20230406180952256

After the service is started, we need to set the address for the Feishu robot to connect to our service. We are required to set an external network address instead of a LAN address.

Here we use the cpolar internal network penetration tool to map the local service under port 9000 to the public network, and it will automatically generate and return the corresponding public network address. You can use this public network address to set up the Feishu robot connection service without public network IP, no need to set up a router, and no public network server is required.

image-20230407123142124

5. Install cpolar intranet penetration

cpolar official website: https://www.cpolar.com/

Visit cpolar official website, register an account, then download and install the windows version of cpolar client.

insert image description here

After the successful installation of cpolar intranet penetration, visit the local port 9200 [ http://localhost:9200 ] on the browser , and log in with the cpolar account.

20230130105810

Click Tunnel Management on the left dashboard - Create Tunnel, create an http tunnel, point to port 9000, which is the service port number on which we run main.go above

  • Tunnel name: you can customize the name, be careful not to duplicate the existing tunnel name
  • Protocol: select http
  • Local address: 9000
  • Domain name type: choose a random domain name for free
  • Region: Select China VIP

click创建

image-20230407115232967

After the tunnel is successfully created, click on the status on the left - online tunnel list, view the generated public network address, and then copy the address

image-20230407120110339

Then open the Feishu robot, we fill in the public network address above, note that the resource path needs to be added /webhook/card, and then click Verify

image-20230407123437256

After clicking verify, there is no prompt indicating success, and we can also see the printed log in powershell

image-20230407123544083

6. Fixed public network address

Since what we just created is a free random temporary tunnel, the public network address it generates will change randomly within 24 hours, which is not suitable for long-term use. So we will fix this public network address next.

Note that the cpolar package needs to be upgraded to the basic package or above, and the bandwidth corresponding to each package is different.

Log in to the background of the cpolar official website, click on the left 预留, select 保留二级子域名, set a second-level subdomain name, click Reserve, and copy the reserved second-level subdomain name after the reservation is successful

insert image description here

After the reservation is successful, copy the reserved second-level subdomain address

insert image description here

Access the local port 9200 [127.0.0.1:9200], log in to the cpolar web UI management interface, click on the tunnel management on the left dashboard - 隧道列表, find the tunnel to be configured, and click on the right编辑

image-20230407124139973

Modify the tunnel information, and configure the successfully reserved second-level subdomain name into the tunnel

  • Domain name type: select a second-level subdomain name
  • Sub Domain: Fill in the reserved sub-domain name

click更新

image-20230407124213948

After the update is complete, open the online tunnel list. At this time, you can see that the public network address has changed, and the address name has also become a reserved second-level subdomain name. Copy it down

image-20230407124241508

Then we open the Feishu robot, 把随机地址换成我们固定的公网地址and click save

image-20230407124400820

Then open the following 事件订阅, also set the public network address into it, change the resource path behind /webhook/event, and then click Save

image-20230407124654752

7. Robot permission configuration

Open permission management, filter permissions, enterim:message

image-20230407130500759

Then enable the following permissions in batches

  • im:resource (get and upload image or file resources)
  • m:message
  • im:message.group_at_msg (Get all messages in the group)
  • im:messagegroup_at_msg:readonly (receive @robot message event in group chat)
  • im:message.p2p_msg (Get the single chat message sent by the user to the robot)
  • im:messagep2p_msg:readonly (read the single chat message sent by the user to the robot)
  • im:message:send_as_bot (get the message of the user @bot in the group)
  • im:chat:readonly (get group information)
  • im:chat (get and update group information)

img

Search im:resource, open this permission

image-20230407130914494

Then enter again im:chatto enable the following two permissions

image-20230407131056766

Then add the event, add the following three events

image-20230407132604092

8. Create version

Click above 创建版本, fill in the version information and update instructions

image-20230407132849387

Then click below to save

image-20230407132927560

After saving successfully申请线上发布

image-20230407133004450

9. Create a test enterprise

Click next to Test Businesses and People, we创建一个测试企业

20230411111702

After creating, click关联应用

image-20230407134212805

Then we switch the account and switch to the test enterprise account we created above

image-20230407134317946

After switching, you can see the robot project we created, click to enter

image-20230407134357061

We can see that the status has been published

image-20230407134536411

Then we have to continue to set the last step, open the credential and basic information, view APPIDand APP Secretthese two values
image-20230407175040631

Then replace the above two values ​​in our configuration file. The configuration file uses the value of the Feishu personal account. Now that the test enterprise account is created, it needs to be changed to the sum of the test enterprise account APPID.APP Secret

image-20230407175654420

Restart after changing

image-20230407175840024

10. Robot Test

Download Feishu, open Feishu, and select the test enterprise account we created above when logging in
image-20230407180058150

then click search

image-20230407180157387

Enter chat, you can see the robot we set up, click

image-20230407180234639

Then you can have a conversation, the robot reply is a bit slow, because it is an overseas interface

image-20230407180602770

Guess you like

Origin blog.csdn.net/m0_73879806/article/details/132613155