60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
|
|
package cmd
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"fmt"
|
|||
|
|
"log"
|
|||
|
|
"net/http"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"github.com/tencent-connect/botgo"
|
|||
|
|
"github.com/tencent-connect/botgo/dto"
|
|||
|
|
"github.com/tencent-connect/botgo/event"
|
|||
|
|
"github.com/tencent-connect/botgo/interaction/webhook"
|
|||
|
|
"github.com/tencent-connect/botgo/token"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
host_ = "0.0.0.0"
|
|||
|
|
port_ = 9000
|
|||
|
|
path_ = "/qqbot"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
func robot() {
|
|||
|
|
|
|||
|
|
credentials := &token.QQBotCredentials{
|
|||
|
|
AppID: "102815288",
|
|||
|
|
AppSecret: "faVQLHD951xurolifcaYWUSQPONMLKKK",
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
log.Println("credentials:", credentials)
|
|||
|
|
tokenSource := token.NewQQBotTokenSource(credentials)
|
|||
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|||
|
|
defer cancel() //释放刷新协程
|
|||
|
|
if err := token.StartRefreshAccessToken(ctx, tokenSource); err != nil {
|
|||
|
|
log.Fatalln(err)
|
|||
|
|
}
|
|||
|
|
// 初始化 openapi,正式环境
|
|||
|
|
botgo.NewOpenAPI(credentials.AppID, tokenSource).WithTimeout(5 * time.Second).SetDebug(true)
|
|||
|
|
|
|||
|
|
// 注册处理函数
|
|||
|
|
_ = event.RegisterHandlers(
|
|||
|
|
|
|||
|
|
GuildDirectMessageHandler(),
|
|||
|
|
)
|
|||
|
|
http.HandleFunc(path_, func(writer http.ResponseWriter, request *http.Request) {
|
|||
|
|
webhook.HTTPHandler(writer, request, credentials)
|
|||
|
|
})
|
|||
|
|
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", host_, port_), nil); err != nil {
|
|||
|
|
log.Fatal("setup server fatal:", err)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GuildDirectMessageHandler 处理频道私信事件
|
|||
|
|
func GuildDirectMessageHandler() event.DirectMessageEventHandler {
|
|||
|
|
return func(event *dto.WSPayload, data *dto.WSDirectMessageData) error {
|
|||
|
|
fmt.Println(data)
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
}
|