飞书
This commit is contained in:
parent
e9f619d8f8
commit
283c975d10
15
feishu/.vscode/launch.json
vendored
Normal file
15
feishu/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
// 使用 IntelliSense 了解相关属性。
|
||||||
|
// 悬停以查看现有属性的描述。
|
||||||
|
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Launch file",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "debug",
|
||||||
|
"program": "${file}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
31
feishu/Handler/P2BotMenuV6.go
Normal file
31
feishu/Handler/P2BotMenuV6.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"feishu/client"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
larkapplication "github.com/larksuite/oapi-sdk-go/v3/service/application/v6"
|
||||||
|
)
|
||||||
|
|
||||||
|
var funcMap map[string]func(ctx context.Context, event *larkapplication.P2BotMenuV6) error
|
||||||
|
|
||||||
|
func P2Handle(ctx context.Context, event *larkapplication.P2BotMenuV6) error {
|
||||||
|
if f, ok := funcMap[*event.Event.EventKey]; ok {
|
||||||
|
f(ctx, event)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
log.Printf("no handler for event: %s", *event.Event.EventKey)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
funcMap = make(map[string]func(ctx context.Context, event *larkapplication.P2BotMenuV6) error)
|
||||||
|
funcMap["operation"] = operation
|
||||||
|
}
|
||||||
|
|
||||||
|
func operation(ctx context.Context, event *larkapplication.P2BotMenuV6) error {
|
||||||
|
openId := *event.Event.Operator.OperatorId.OpenId
|
||||||
|
client.C.SendMsg(openId, "operation")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
61
feishu/client/c.go
Normal file
61
feishu/client/c.go
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
lark "github.com/larksuite/oapi-sdk-go/v3"
|
||||||
|
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
|
||||||
|
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
type client struct {
|
||||||
|
*lark.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
var C *client
|
||||||
|
|
||||||
|
const (
|
||||||
|
AppId = "cli_a726aa9056f1d00b"
|
||||||
|
AppSecret = "KWteGycoqAYdcqbTeCcUNbteVmpcTOCU"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
C = &client{lark.NewClient(AppId, AppSecret)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetClient() *client {
|
||||||
|
return C
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *client) SendMsg(open_id, content string) error {
|
||||||
|
// 创建 Client
|
||||||
|
b, _ := json.Marshal(map[string]string{"text": content})
|
||||||
|
uuidVal := uuid.New().String()
|
||||||
|
req := larkim.NewCreateMessageReqBuilder().
|
||||||
|
ReceiveIdType(`open_id`).
|
||||||
|
Body(larkim.NewCreateMessageReqBodyBuilder().
|
||||||
|
ReceiveId(open_id).
|
||||||
|
MsgType(`text`).
|
||||||
|
Content(string(b)).
|
||||||
|
Uuid(uuidVal).
|
||||||
|
Build()).
|
||||||
|
Build()
|
||||||
|
|
||||||
|
// 发起请求
|
||||||
|
resp, err := c.Im.V1.Message.Create(context.Background(), req)
|
||||||
|
|
||||||
|
// 处理错误
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// 服务端错误处理
|
||||||
|
if !resp.Success() {
|
||||||
|
fmt.Printf("logId: %s, error response: \n%s", resp.RequestId(), larkcore.Prettify(resp.CodeError))
|
||||||
|
return fmt.Errorf("logId: %s, error response: \n%s", resp.RequestId(), larkcore.Prettify(resp.CodeError))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
81
feishu/client/h.go
Normal file
81
feishu/client/h.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"feishu/data"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SendMsg(data []*data.Operation) error {
|
||||||
|
retainStr := ""
|
||||||
|
for _, v := range data {
|
||||||
|
retainStr += fmt.Sprintf("| %s | %d | %d | %.2f | %.2f%% | %.2f%% | %d | %d |\n", v.Date.Format("2006-01-02"), v.Register, v.Login, v.Recharge, float64(v.SecondRemain)/float64(v.Recharge), float64(v.ThirdRemain)/float64(v.Recharge), v.SeventhRemain, v.ThirtiethRemain)
|
||||||
|
}
|
||||||
|
str := fmt.Sprintf(`
|
||||||
|
# 日期 %s 😁
|
||||||
|
-----------------------------
|
||||||
|
## 昨日数据
|
||||||
|
|
||||||
|
- 注册:%d
|
||||||
|
- 充值:%.2f
|
||||||
|
- 登录:%d
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
## 留存数据
|
||||||
|
|
||||||
|
| Date | Reg | Login | Pay | Retain2 | Retain3 | Retain7 | Retain30 |
|
||||||
|
| -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- |
|
||||||
|
%s
|
||||||
|
|
||||||
|
## 玩家数据
|
||||||
|
|
||||||
|
| Uid | Level | Exp | Diamond | Star | Energy |
|
||||||
|
| -------- | -------- | -------- | -------- | -------- | -------- |
|
||||||
|
| 1 | 2 | 3 | 4 | 5 | 6 |
|
||||||
|
| 1 | 2 | 3 | 4 | 5 | 6 |
|
||||||
|
`, time.Now().In(time.UTC).Format("2006-01-02"), data[0].Register, data[0].Recharge, data[0].Login, retainStr)
|
||||||
|
// 创建请求体
|
||||||
|
payload := map[string]interface{}{
|
||||||
|
"msg_type": "interactive",
|
||||||
|
"card": map[string]interface{}{
|
||||||
|
"type": "template",
|
||||||
|
"data": map[string]interface{}{
|
||||||
|
"template_id": "AAqBcfmUwQya1",
|
||||||
|
"template_version_name": "1.0.1",
|
||||||
|
"template_variable": map[string]interface{}{
|
||||||
|
"msg": str,
|
||||||
|
"appName": "merge_pet_london",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
payloadBytes, err := json.Marshal(payload)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建HTTP请求
|
||||||
|
req, err := http.NewRequest("POST", "https://open.feishu.cn/open-apis/bot/v2/hook/64bad1f3-3a41-4dca-9037-399067ffb252", bytes.NewBuffer(payloadBytes))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
// 发送请求
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// 检查响应状态码
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return fmt.Errorf("failed to send message, status code: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
47
feishu/data/operation.go
Normal file
47
feishu/data/operation.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"feishu/util"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Operation struct {
|
||||||
|
Date time.Time `db:"Date"`
|
||||||
|
Register int `db:"Register"`
|
||||||
|
SecondRemain int `db:"SecondRemain"`
|
||||||
|
ThirdRemain int `db:"ThirdRemain"`
|
||||||
|
SeventhRemain int `db:"SeventhRemain"`
|
||||||
|
ThirtiethRemain int `db:"ThirtiethRemain"`
|
||||||
|
Recharge float64 `db:"Recharge"`
|
||||||
|
Login int `db:"Login"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Uid int `db:"Uid"`
|
||||||
|
Level int `db:"Level"`
|
||||||
|
Exp int `db:"Exp"`
|
||||||
|
Diamond int `db:"Diamond"`
|
||||||
|
Star int `db:"Star"`
|
||||||
|
Energy int `db:"Energy"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOperation() ([]*Operation, error) {
|
||||||
|
Db, err := util.MySQL()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("GetOperation MySQL error: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer Db.Close()
|
||||||
|
Result := []*Operation{}
|
||||||
|
ZeroTimestamp := util.ZeroTimestampByTz("Europe/London") - 86400
|
||||||
|
ZeroTime := time.Unix(ZeroTimestamp, 0).In(time.UTC)
|
||||||
|
StartDate := ZeroTime.AddDate(0, 0, -7).Format("2006-01-02")
|
||||||
|
EndDate := ZeroTime.Format("2006-01-02")
|
||||||
|
err = Db.Select(&Result, "SELECT `Date`, `Register`, `SecondRemain`, `ThirdRemain`, `SeventhRemain`, `ThirtiethRemain`, `Recharge`, `Login` FROM remain where `Date` >= ? and `Date` <= ? order by `Date` desc", StartDate, EndDate)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("GetOperation Select error: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return Result, nil
|
||||||
|
}
|
||||||
16
feishu/go.mod
Normal file
16
feishu/go.mod
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
module feishu
|
||||||
|
|
||||||
|
go 1.23.1
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/go-sql-driver/mysql v1.8.1
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
|
github.com/jmoiron/sqlx v1.4.0
|
||||||
|
github.com/larksuite/oapi-sdk-go/v3 v3.4.9
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
|
github.com/gorilla/websocket v1.5.0 // indirect
|
||||||
|
)
|
||||||
47
feishu/go.sum
Normal file
47
feishu/go.sum
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
|
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||||
|
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||||
|
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||||
|
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
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/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
|
||||||
|
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
|
||||||
|
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.4.9 h1:ZzsPtdF2tsLbocQdKLNFwFCzWfwmKtD4kbyx7to++M0=
|
||||||
|
github.com/larksuite/oapi-sdk-go/v3 v3.4.9/go.mod h1:ZEplY+kwuIrj/nqw5uSCINNATcH3KdxSN7y+UxYY5fI=
|
||||||
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||||
|
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=
|
||||||
154
feishu/main.go
Normal file
154
feishu/main.go
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
handler "feishu/Handler"
|
||||||
|
"feishu/client"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
|
||||||
|
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher"
|
||||||
|
larkapplication "github.com/larksuite/oapi-sdk-go/v3/service/application/v6"
|
||||||
|
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
|
||||||
|
larkws "github.com/larksuite/oapi-sdk-go/v3/ws"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// 注册事件回调,OnP2MessageReceiveV1 为接收消息 v2.0;OnCustomizedEvent 内的 message 为接收消息 v1.0。
|
||||||
|
eventHandler := dispatcher.NewEventDispatcher("", "").
|
||||||
|
OnP2MessageReceiveV1(func(ctx context.Context, event *larkim.P2MessageReceiveV1) error {
|
||||||
|
// fmt.Printf("[ OnP2MessageReceiveV1 access ], data: %s\n", larkcore.Prettify(event))
|
||||||
|
Content := *event.Event.Message.Content
|
||||||
|
OpenId := *event.Event.Sender.SenderId.OpenId
|
||||||
|
r := map[string]string{}
|
||||||
|
if Content != "" {
|
||||||
|
json.Unmarshal([]byte(Content), &r)
|
||||||
|
go SendMsg(OpenId, r["text"])
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}).
|
||||||
|
OnP2BotMenuV6(func(ctx context.Context, event *larkapplication.P2BotMenuV6) error {
|
||||||
|
return handler.P2Handle(ctx, event)
|
||||||
|
})
|
||||||
|
|
||||||
|
// 创建Client
|
||||||
|
cli := larkws.NewClient(client.AppId, client.AppSecret,
|
||||||
|
larkws.WithEventHandler(eventHandler),
|
||||||
|
larkws.WithLogLevel(larkcore.LogLevelDebug),
|
||||||
|
)
|
||||||
|
// 启动客户端
|
||||||
|
err := cli.Start(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendMsg(open_id, content string) {
|
||||||
|
// 创建 Client
|
||||||
|
|
||||||
|
uuidVal := uuid.New().String()
|
||||||
|
Answer := AI(content)
|
||||||
|
b, _ := json.Marshal(map[string]string{"text": Answer})
|
||||||
|
// 创建请求对象
|
||||||
|
req := larkim.NewCreateMessageReqBuilder().
|
||||||
|
ReceiveIdType(`open_id`).
|
||||||
|
Body(larkim.NewCreateMessageReqBodyBuilder().
|
||||||
|
ReceiveId(open_id).
|
||||||
|
MsgType(`text`).
|
||||||
|
Content(string(b)).
|
||||||
|
Uuid(uuidVal).
|
||||||
|
Build()).
|
||||||
|
Build()
|
||||||
|
|
||||||
|
// 发起请求
|
||||||
|
resp, err := client.C.Im.V1.Message.Create(context.Background(), req)
|
||||||
|
|
||||||
|
// 处理错误
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 服务端错误处理
|
||||||
|
if !resp.Success() {
|
||||||
|
fmt.Printf("logId: %s, error response: \n%s", resp.RequestId(), larkcore.Prettify(resp.CodeError))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type AiMessage struct {
|
||||||
|
Event string `json:"event"`
|
||||||
|
Conversation_id string `json:"conversation_id"`
|
||||||
|
Message_id string `json:"message_id"`
|
||||||
|
Create_at int `json:"create_at"`
|
||||||
|
Task_id string `json:"task_id"`
|
||||||
|
Id string `json:"id"`
|
||||||
|
Answer string `json:"answer"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func AI(message string) string {
|
||||||
|
//escapedMessage := strings.ReplaceAll(message, `"`, `\"`)
|
||||||
|
body := `{
|
||||||
|
"inputs": {},
|
||||||
|
"query": "` + message + `",
|
||||||
|
"response_mode": "streaming",
|
||||||
|
"conversation_id": "",
|
||||||
|
"user": "abc-123",
|
||||||
|
"files": []
|
||||||
|
}`
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", "http://8.155.13.51/v1/chat-messages", strings.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("创建请求失败: %v", err)
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Authorization", "Bearer app-14eZWBFFIyUSZi7OYrnOGhSR")
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
reader := bufio.NewReader(resp.Body)
|
||||||
|
var str string
|
||||||
|
for {
|
||||||
|
var message AiMessage
|
||||||
|
line, readErr := reader.ReadString('\n')
|
||||||
|
if readErr != nil {
|
||||||
|
if readErr == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
log.Fatalf("读取响应时出错: %v", readErr)
|
||||||
|
}
|
||||||
|
newLine := strings.TrimPrefix(line, "data:")
|
||||||
|
|
||||||
|
// 解析 JSON 数据
|
||||||
|
if err := json.Unmarshal([]byte(newLine), &message); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if message.Event == "message" {
|
||||||
|
str += message.Answer
|
||||||
|
}
|
||||||
|
// 处理数据
|
||||||
|
|
||||||
|
//fmt.Print("收到数据: ", line)
|
||||||
|
}
|
||||||
|
|
||||||
|
r := regexp.MustCompile(`<think>[\s\S]*?<\/think>`)
|
||||||
|
str = r.ReplaceAllString(str, "")
|
||||||
|
str = strings.TrimLeft(str, "\n")
|
||||||
|
if str == "" {
|
||||||
|
str = "我不知道你在说什么"
|
||||||
|
}
|
||||||
|
return str
|
||||||
|
}
|
||||||
91
feishu/unit_test.go
Normal file
91
feishu/unit_test.go
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"encoding/json"
|
||||||
|
"feishu/client"
|
||||||
|
"feishu/data"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type message struct {
|
||||||
|
Event string `json:"event"`
|
||||||
|
Conversation_id string `json:"conversation_id"`
|
||||||
|
Message_id string `json:"message_id"`
|
||||||
|
Create_at int `json:"create_at"`
|
||||||
|
Task_id string `json:"task_id"`
|
||||||
|
Id string `json:"id"`
|
||||||
|
Answer string `json:"answer"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCard(t *testing.T) {
|
||||||
|
data, err := data.GetOperation()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetOperation error: %v", err)
|
||||||
|
}
|
||||||
|
err = client.SendMsg(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("SendMsg error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...existing code...
|
||||||
|
func TestHTTPPost(t *testing.T) {
|
||||||
|
body := `{
|
||||||
|
"inputs": {},
|
||||||
|
"query": "游戏订单提交失败",
|
||||||
|
"response_mode": "streaming",
|
||||||
|
"conversation_id": "",
|
||||||
|
"user": "abc-123",
|
||||||
|
"files": []
|
||||||
|
}`
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", "http://8.155.13.51/v1/chat-messages", strings.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("创建请求失败: %v", err)
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Authorization", "Bearer app-14eZWBFFIyUSZi7OYrnOGhSR")
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
reader := bufio.NewReader(resp.Body)
|
||||||
|
var str string
|
||||||
|
for {
|
||||||
|
var message message
|
||||||
|
line, readErr := reader.ReadString('\n')
|
||||||
|
if readErr != nil {
|
||||||
|
if readErr == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
t.Fatalf("读取响应时出错: %v", readErr)
|
||||||
|
}
|
||||||
|
newLine := strings.TrimPrefix(line, "data:")
|
||||||
|
|
||||||
|
// 解析 JSON 数据
|
||||||
|
if err := json.Unmarshal([]byte(newLine), &message); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if message.Event == "message" {
|
||||||
|
str += message.Answer
|
||||||
|
}
|
||||||
|
// 处理数据
|
||||||
|
|
||||||
|
//fmt.Print("收到数据: ", line)
|
||||||
|
}
|
||||||
|
|
||||||
|
r := regexp.MustCompile(`<think>[\s\S]*?<\/think>`)
|
||||||
|
str = r.ReplaceAllString(str, "")
|
||||||
|
|
||||||
|
fmt.Print("收到数据: ", str)
|
||||||
|
}
|
||||||
34
feishu/util/msyql.go
Normal file
34
feishu/util/msyql.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MYSQL_USER = "root"
|
||||||
|
MYSQL_PASS = "Z4rf7eZZe500dxa"
|
||||||
|
MYSQL_HOST = "rm-f8zd2030feam53n43io.mysql.rds.aliyuncs.com"
|
||||||
|
MYSQL_PORT = 3306
|
||||||
|
MYSQL_DATABASE = "merge_pet_london"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MySQL() (*sqlx.DB, error) {
|
||||||
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", MYSQL_USER, MYSQL_PASS, MYSQL_HOST, MYSQL_PORT, MYSQL_DATABASE)
|
||||||
|
db, err := sqlx.Open("mysql", dsn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func MYSQL2() (*sqlx.DB, error) {
|
||||||
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", MYSQL_USER, MYSQL_PASS, "8.208.47.208", MYSQL_PORT, "merge_pet_london_1")
|
||||||
|
db, err := sqlx.Open("mysql", dsn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
10
feishu/util/util.go
Normal file
10
feishu/util/util.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
func ZeroTimestampByTz(tz string) int64 {
|
||||||
|
loc, _ := time.LoadLocation(tz)
|
||||||
|
now := time.Now().In(loc)
|
||||||
|
midnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||||
|
return midnight.Unix()
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user