Skip to content

Commit

Permalink
feat: 回声机器人&审批机器人
Browse files Browse the repository at this point in the history
  • Loading branch information
anxinfeng committed Dec 5, 2024
1 parent 32ceb70 commit bff3180
Show file tree
Hide file tree
Showing 62 changed files with 2,804 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This is used to open Feishu code samples to help developers quickly use Feishu c
- [Quickly develop a web app](web_app_with_jssdk/python/README.md)
- [Quickly create a login-free web app](web_app_with_auth/python/README.md)
- [React+nodejs quickly implements web applications and free web applications](js_web_app/README.md)
- Echo bot [go](echo_robot/go/README.md) [python](echo_robot/python/README.md) [java](echo_robot/java/README.md) [nodejs](echo_robot/nodejs/README.md)
- Interactive bot [go](interactive_bot/go/README.md) [python](interactive_bot/python/README.md) [java](interactive_bot/java/README.md) [nodejs](interactive_bot/nodejs/README.md)

## License
- Apache 2.0
2 changes: 2 additions & 0 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- [快速实现网页应用免登](web_app_with_auth/python/README.zh.md)
- [react+nodejs快速实现网页应用和网页免登应用](react_and_nodejs/web_app/README.zh.md)
- [react+nodejs快速开发机器人](react_and_nodejs/robot/README.zh.md)
- 回声机器人 [go](echo_robot/go/README.md) [python](echo_robot/python/README.md) [java](echo_robot/java/README.md) [nodejs](echo_robot/nodejs/README.md)
- 审批机器人 [go](interactive_bot/go/README.md) [python](interactive_bot/python/README.md) [java](interactive_bot/java/README.md) [nodejs](interactive_bot/nodejs/README.md)

## License
- Apache 2.0
12 changes: 12 additions & 0 deletions echo_bot/go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 回声机器人

## 效果
![](./assets/image.png)

响应用户输入的消息

## 启动项目

macOS/Linux: `APP_ID=<app_id> APP_SECRET=<app_secret> ./bootstrap.sh`

Windows: `set APP_ID=<app_id>&set APP_SECRET=<app_secret>&bootstrap.bat`
Binary file added echo_bot/go/assets/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions echo_bot/go/bootstrap.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@echo off

set GOPROXY=https://mirrors.aliyun.com/goproxy/
set GO111MODULE=on

REM 检查 Go 是否已安装 / Check if Go is installed

where go >nul 2>nul
IF ERRORLEVEL 1 (
echo 当前设备未安装Go语言环境, 请安装后重试。/ The current device is not installed with Go language environment, please install it and try again.
echo 可参考 ./go-setup.md 安装Go语言环境。/ Refer to./go-setup.md for Go installation instructions.
pause
EXIT /b 1
) ELSE (
echo Go 已安装,版本: / Go is already installed, version:
call go version
)

REM 安装项目依赖 / Install project dependencies
IF EXIST "go.mod" (
echo 开始安装项目依赖... / Installing project dependencies...
call go mod tidy
) ELSE (
echo 未找到 go.mod 文件,跳过依赖安装。/ No go.mod file found, skipping dependency installation.
pause
EXIT /b 1
)

echo 启动项目... / Starting the project...
go run main.go

28 changes: 28 additions & 0 deletions echo_bot/go/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

export GOPROXY=https://mirrors.aliyun.com/goproxy/
export GO111MODULE=on

go env GOPROXY
# 检查 Go 是否已安装 / Check if Go is installed
if ! command -v go &> /dev/null; then
echo "当前设备未安装Go语言环境, 请安装后重试。/ Go not installed, please install Go before retrying."
echo "可参考 ./go-setup.md 安装Go语言环境。/ Refer to./go-setup.md for Go installation instructions."
exit 1
else
echo "Go 已安装,版本: $(go version) / Go is already installed, version: $(go version)"
fi


# 安装项目依赖 / Install project dependencies
if [ -f "go.mod" ]; then
echo "开始安装项目依赖... / Installing project dependencies..."
go mod tidy
else
echo "未找到 go.mod 文件,跳过依赖安装。/ No go.mod file found, skipping dependency installation."
fi


# 启动项目 / Start the project
echo "启动项目... / Starting the project..."
go run main.go
92 changes: 92 additions & 0 deletions echo_bot/go/go-setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
** 这份文档将指导您完成 Go 语言环境的安装 **

# 以下为具体安装步骤

## Windows

### 1. 下载 Go 语言安装包

- 访问 Go 语言的官方下载页面:[https://golang.google.cn/dl/](https://golang.google.cn/dl/)
- 选择适用于 Windows 的安装包,例如最新版本的`go1.23.3.windows-386.msi`(请替换为您需要的版本号)。
- 点击下载链接,等待下载完成。

### 2. 安装 Go 语言

- 找到下载的安装包,通常位于浏览器的下载目录中。
- 双击安装文件,按照安装向导的指示进行操作:
- 点击“Next”接受许可协议。
- 在选择安装路径窗口,可以选择默认安装到`C:\Go`目录,或者修改为其他路径。然后点击“Next”。
- 在接下来的窗口可以选择安装组件,通常默认全选即可。点击“Next”继续。
- 最后一个窗口是概要信息,点击“Install”开始安装。
- 安装完成后,点击“Finish”关闭安装向导。

### 3. 配置环境变量

- 使用 Windows 键+R 组合键打开“运行”窗口,输入`sysdm.cpl`,回车打开系统属性对话框。
- 在“高级”标签页下点击“环境变量”按钮打开环境变量对话框。
- 在“系统变量”中找到`Path`变量,双击编辑,在变量值的最后追加`;C:\Go\bin`(注意路径要换成您的实际 Go 安装路径)。
- 添加`GOROOT`变量,变量值设置为 Go 的安装路径,如`C:\Go`
- 添加`GOPATH`变量,变量值设置为您的代码工作区路径,如`C:\GoWork`

### 4. 校验是否配置成功

- 打开新的命令提示符(cmd),输入`go version`,如果输出显示 Go 的版本信息,则表示安装成功。
- 输入`go env`可以查看 Go 的环境变量配置情况。

## macOS

### 步骤 1:安装 Homebrew(如果尚未安装)

如果您的 Mac 尚未安装 Homebrew,可以通过以下命令进行安装:

```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```

安装完成后,您可以通过执行`brew --version`来验证 Homebrew 是否安装成功。

### 步骤 2:使用 Homebrew 安装 Go

在 ARM 架构的 Mac 上,您可以直接使用 Homebrew 来安装 Go,Homebrew 会自动适配 ARM 架构:

```bash
brew install go
```

这个命令会下载并安装适合您 Mac 芯片架构的 Go 版本。

### 步骤 3:验证 Go 安装

安装完成后,您可以通过执行以下命令来验证 Go 是否安装成功:

```bash
go version
```

## Linux

以下为基于 Debian 的系统的 linux 上安装 Go 语言环境 的步骤:

### 步骤 1:更新包索引

打开终端,首先更新您的包索引:

```bash
sudo apt update
```

### 步骤 2:安装 Go

使用`apt`安装 Go:

```bash
sudo apt install golang-go
```

### 步骤 3:验证安装

安装完成后,您可以通过运行以下命令来验证 Go 是否安装成功:

```bash
go version
```
10 changes: 10 additions & 0 deletions echo_bot/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module main

go 1.22.5

require github.com/larksuite/oapi-sdk-go/v3 v3.3.5

require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
)
35 changes: 35 additions & 0 deletions echo_bot/go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/larksuite/oapi-sdk-go/v3 v3.3.5 h1:ItBdUBAYDbH5QleeKYm08vgEyHCbtt/HXX+y9aKSve0=
github.com/larksuite/oapi-sdk-go/v3 v3.3.5/go.mod h1:ZEplY+kwuIrj/nqw5uSCINNATcH3KdxSN7y+UxYY5fI=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
116 changes: 116 additions & 0 deletions echo_bot/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"context"
"encoding/json"
"fmt"
"os"

lark "github.com/larksuite/oapi-sdk-go/v3"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher"
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
larkws "github.com/larksuite/oapi-sdk-go/v3/ws"
)

func main() {
app_id := os.Getenv("APP_ID")
app_secret := os.Getenv("APP_SECRET")

/**
* 创建 LarkClient 对象,用于请求OpenAPI。
* Create LarkClient object for requesting OpenAPI
*/
client := lark.NewClient(app_id, app_secret)

/**
* 注册事件处理器。
* Register event handler.
*/
eventHandler := dispatcher.NewEventDispatcher("", "").
/**
* 注册接收消息事件,处理接收到的消息。
* Register event handler to handle received messages.
* https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/events/receive
*/
OnP2MessageReceiveV1(func(ctx context.Context, event *larkim.P2MessageReceiveV1) error {
fmt.Printf("[OnP2MessageReceiveV1 access], data: %s\n", larkcore.Prettify(event))
/**
* 解析用户发送的消息。
* Parse the message sent by the user.
*/
var respContent map[string]string
err := json.Unmarshal([]byte(*event.Event.Message.Content), &respContent)
/**
* 检查消息类型是否为文本
* Check if the message type is text
*/
if err != nil || *event.Event.Message.MessageType != "text" {
respContent = map[string]string{
"text": "解析消息失败,请发送文本消息\nparse message failed, please send text message",
}
}

/**
* 构建回复消息
* Build reply message
*/
content := larkim.NewTextMsgBuilder().
TextLine("收到你发送的消息: " + respContent["text"]).
TextLine("Received message: " + respContent["text"]).
Build()

if *event.Event.Message.ChatType == "p2p" {
/**
* 使用SDK调用发送消息接口。 Use SDK to call send message interface.
* https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/create
*/
resp, err := client.Im.Message.Create(context.Background(), larkim.NewCreateMessageReqBuilder().
ReceiveIdType(larkim.ReceiveIdTypeChatId). // 消息接收者的 ID 类型,设置为会话ID。 ID type of the message receiver, set to chat ID.
Body(larkim.NewCreateMessageReqBodyBuilder().
MsgType(larkim.MsgTypeText). // 设置消息类型为文本消息。 Set message type to text message.
ReceiveId(*event.Event.Message.ChatId). // 消息接收者的 ID 为消息发送的会话ID。 ID of the message receiver is the chat ID of the message sending.
Content(content).
Build()).
Build())

if err != nil || !resp.Success() {
fmt.Println(err)
fmt.Println(resp.Code, resp.Msg, resp.RequestId())
return nil
}

} else {
/**
* 使用SDK调用回复消息接口。 Use SDK to call send message interface.
* https://open.feishu.cn/document/server-docs/im-v1/message/reply
*/
resp, err := client.Im.Message.Reply(context.Background(), larkim.NewReplyMessageReqBuilder().
MessageId(*event.Event.Message.MessageId).
Body(larkim.NewReplyMessageReqBodyBuilder().
MsgType(larkim.MsgTypeText). // 设置消息类型为文本消息。 Set message type to text message.
Content(content).
Build()).
Build())
if err != nil || !resp.Success() {
fmt.Printf("logId: %s, error response: \n%s", resp.RequestId(), larkcore.Prettify(resp.CodeError))
return nil
}
}

return nil
})

/**
* 启动长连接,并注册事件处理器。
* Start long connection and register event handler.
*/
cli := larkws.NewClient(app_id, app_secret,
larkws.WithEventHandler(eventHandler),
larkws.WithLogLevel(larkcore.LogLevelDebug),
)
err := cli.Start(context.Background())
if err != nil {
panic(err)
}
}
2 changes: 2 additions & 0 deletions echo_bot/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
.idea
13 changes: 13 additions & 0 deletions echo_bot/java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 回声机器人

## 效果

![](./assets/image.png)

响应用户输入的消息

## 启动项目

macOS/Linux: `APP_ID=<app_id> APP_SECRET=<app_secret> ./bootstrap.sh`

Windows: `set APP_ID=<app_id>&set APP_SECRET=<app_secret>&bootstrap.bat`
Binary file added echo_bot/java/assets/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit bff3180

Please sign in to comment.