Skip to content

Commit

Permalink
chore: ⬆️ dingtalk-stream-sdk-go 依赖版本升级 (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryajf authored Jun 11, 2023
1 parent c4a7693 commit a564be3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 146 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/gin-gonic/gin v1.9.0
github.com/glebarez/sqlite v1.7.0
github.com/go-resty/resty/v2 v2.7.0
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.0.1
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.0.4
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/sashabaranov/go-openai v1.6.1
github.com/solywsh/chatgpt v0.0.14
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs=
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.0.1 h1:F7c4ZWg5FuL0giOVg0slzPmYLwbuQqTjsu5BkUL6VEU=
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.0.1/go.mod h1:ln3IqPYYocZbYvl9TAOrG/cxGR9xcn4pnZRLdCTEGEU=
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.0.4 h1:34Ox70UZ+7TIg3Qho1QqQOUtRTo8eO3f8XYE+P27PiQ=
github.com/open-dingtalk/dingtalk-stream-sdk-go v0.0.4/go.mod h1:ln3IqPYYocZbYvl9TAOrG/cxGR9xcn4pnZRLdCTEGEU=
github.com/pandodao/tokenizer-go v0.2.0 h1:NhfI8fGvQkDld2cZCag6NEU3pJ/ugU9zoY1R/zi9YCs=
github.com/pandodao/tokenizer-go v0.2.0/go.mod h1:t6qFbaleKxbv0KNio2XUN/mfGM5WKv4haPXDQWVDG00=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
Expand Down
45 changes: 22 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,27 @@ import (
"github.com/gin-gonic/gin"
"github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot"
"github.com/open-dingtalk/dingtalk-stream-sdk-go/client"
loger "github.com/open-dingtalk/dingtalk-stream-sdk-go/logger"
"github.com/open-dingtalk/dingtalk-stream-sdk-go/payload"
"github.com/open-dingtalk/dingtalk-stream-sdk-go/utils"
)

func init() {
// 初始化加载配置,数据库,模板等
public.InitSvc()
// 指定日志等级
logger.InitLogger(public.Config.LogLevel)
}

func main() {
if public.Config.RunMode == "http" {
StartHttp()
} else {
for _, credential := range public.Config.Credentials {
StartStream(credential.ClientID, credential.ClientSecret)
}
logger.Info("🚀 The Server Is Running On Stream Mode")
select {}
}
}

// 启动为 stream 模式
func StartStream(clientId, clientSecret string) {
receiver := NewChatReceiver(clientId, clientSecret)
loger.SetLogger(loger.NewStdTestLogger())
cli := client.NewStreamClient(
client.WithAppCredential(client.NewAppCredentialConfig(clientId, clientSecret)),
client.WithUserAgent(client.NewDingtalkGoSDKUserAgent()),
client.WithSubscription(utils.SubscriptionTypeKCallback, payload.BotMessageCallbackTopic, chatbot.NewDefaultChatBotFrameHandler(receiver.OnChatReceive).OnEventReceived),
)
err := cli.Start(context.Background())
if err != nil {
panic(err)
}

defer cli.Close()

}

type ChatReceiver struct {
clientId string
clientSecret string
Expand All @@ -66,7 +49,23 @@ func NewChatReceiver(clientId, clientSecret string) *ChatReceiver {
}
}

func (r *ChatReceiver) OnChatReceive(ctx context.Context, data *chatbot.BotCallbackDataModel) (err error) {
// 启动为 stream 模式
func StartStream(clientId, clientSecret string) {
receiver := NewChatReceiver(clientId, clientSecret)
cli := client.NewStreamClient(client.WithAppCredential(client.NewAppCredentialConfig(clientId, clientSecret)))

//注册callback类型的处理函数
cli.RegisterChatBotCallbackRouter(receiver.OnChatBotMessageReceived)

err := cli.Start(context.Background())
if err != nil {
logger.Fatal("strar stream failed: %v\n", err)
}
defer cli.Close()
}

// OnChatBotMessageReceived 简单的应答机器人实现
func (r *ChatReceiver) OnChatBotMessageReceived(ctx context.Context, data *chatbot.BotCallbackDataModel) ([]byte, error) {
msgObj := dingbot.ReceiveMsg{
ConversationID: data.ConversationId,
AtUsers: []struct {
Expand All @@ -93,7 +92,7 @@ func (r *ChatReceiver) OnChatReceive(ctx context.Context, data *chatbot.BotCallb
c.Set(public.DingTalkClientIdKeyName, clientId)
DoRequest(msgObj, &c)

return nil
return []byte(""), nil
}

func StartHttp() {
Expand Down
84 changes: 0 additions & 84 deletions public/example_bot.go

This file was deleted.

36 changes: 0 additions & 36 deletions public/example_event.go

This file was deleted.

0 comments on commit a564be3

Please sign in to comment.