From d3c27236ba0f37b109ffdd572fc1d455f9f7bdde Mon Sep 17 00:00:00 2001 From: jiangjian Date: Thu, 18 May 2023 14:45:50 +0800 Subject: [PATCH] =?UTF-8?q?feat=20#1=20=E5=A2=9E=E5=8A=A0readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 57 +++++++++++++++++++++++++++++++++++++++- example/example_bot.go | 4 +-- example/example_event.go | 4 +-- example/example_main.go | 12 ++++++++- 4 files changed, 71 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 78cb766..cffbd94 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,57 @@ -# dingtalk-stream-sdk-go +# DingTalk Stream Mode 介绍 + Go SDK for DingTalk Stream Mode API, Compared with the webhook mode, it is easier to access the DingTalk chatbot + +钉钉支持 Stream 模式接入事件推送、机器人收消息以及卡片回调,该 SDK 实现了 Stream 模式。相比 Webhook 模式,Stream 模式可以更简单的接入各类事件和回调。 + +## 快速开始 + +### 准备工作 + +* Go语言开发环境,https://www.go.dev/ +* 钉钉开发者账号,具备创建企业内部应用的权限,详见[成为钉钉开发者](https://open.dingtalk.com/document/orgapp/become-a-dingtalk-developer) + +### 快速开始指南 + +1、创建企业内部应用 + +进入[钉钉开发者后台](https://open-dev.dingtalk.com/),创建企业内部应用,获取ClientID(即 AppKey)和ClientSecret( 即AppSecret)。 + +发布应用:在开发者后台左侧导航中,点击“版本管理与发布”,点击“确认发布”,并在接下来的可见范围设置中,选择“全部员工”,或者按需选择部分员工。 + + +2、Stream 模式的机器人(可选) + +如果不需要使用机器人功能的话,可以不用创建。 + +在应用管理的左侧导航中,选择“消息推送”,打开机器人能力,设置机器人基本信息。 + +注意:消息接收模式中,选择 “Stream 模式” + +![Stream 模式](https://img.alicdn.com/imgextra/i3/O1CN01XL4piO1lkYX2F6sW6_!!6000000004857-0-tps-896-522.jpg) + +点击“点击调试”按钮,可以创建测试群进行测试。 + +3、启动应用 + +修改参数,启动应用 +```Shell +go run example/*.go -client_id "your-client-id" -client_secret "your-client-secret" +``` + +测试效果: +![calcbot](https://s1.ax1x.com/2023/05/16/p92jjIJ.png) + +### 事件订阅切换到 Stream 模式(可选) + +进入钉钉开发者后台,选择企业内部应用,在应用管理的左侧导航中,选择“事件与回调”。 +“订阅管理”中,“推送方式”选项中,选择 “Stream 模式”,并保存 + + +### 技术支持 + +可以搜索共创群,答疑交流。共创群ID:35365014813 (钉钉搜索群号入群); + +也可以扫码入群: + +![扫码入群](https://gw.alicdn.com/imgextra/i1/O1CN01Cl10lw1OrfW9LdIgQ_!!6000000001759-0-tps-585-765.jpg) diff --git a/example/example_bot.go b/example/example_bot.go index 3b1dfb6..9638275 100644 --- a/example/example_bot.go +++ b/example/example_bot.go @@ -63,11 +63,11 @@ func OnChatReceive(ctx context.Context, data *chatbot.BotCallbackDataModel) erro return nil } -func RunBotListener() { +func RunBotListener(clientId, clientSecret string) { logger.SetLogger(logger.NewStdTestLogger()) cli := client.NewStreamClient( - client.WithAppCredential(client.NewAppCredentialConfig("your-client-id", "your-client-secret")), + client.WithAppCredential(client.NewAppCredentialConfig(clientId, clientSecret)), client.WithUserAgent(client.NewDingtalkGoSDKUserAgent()), client.WithSubscription(utils.SubscriptionTypeKCallback, payload.BotMessageCallbackTopic, chatbot.NewDefaultChatBotFrameHandler(OnChatReceive).OnEventReceived), ) diff --git a/example/example_event.go b/example/example_event.go index 5844dca..f0ffbbb 100644 --- a/example/example_event.go +++ b/example/example_event.go @@ -13,13 +13,13 @@ import ( * @Date 2023/3/22 18:30 */ -func RunEventListener() { +func RunEventListener(clientId, clientSecret string) { logger.SetLogger(logger.NewStdTestLogger()) eventHandler := event.NewDefaultEventFrameHandler(event.EventHandlerDoNothing) cli := client.NewStreamClient( - client.WithAppCredential(client.NewAppCredentialConfig("your-client-id", "your-client-secret")), + client.WithAppCredential(client.NewAppCredentialConfig(clientId, clientSecret)), client.WithUserAgent(client.NewDingtalkGoSDKUserAgent()), client.WithSubscription(utils.SubscriptionTypeKEvent, "*", eventHandler.OnEventReceived), ) diff --git a/example/example_main.go b/example/example_main.go index 7be3220..6997e80 100644 --- a/example/example_main.go +++ b/example/example_main.go @@ -1,12 +1,22 @@ package main +import ( + "flag" +) + /** * @Author linya.jj * @Date 2023/3/22 18:30 */ func main() { - RunBotListener() + var clientId, clientSecret string + flag.StringVar(&clientId, "client_id", "", "your-client-id") + flag.StringVar(&clientSecret, "client_secret", "", "your-client-secret") + + flag.Parse() + + RunBotListener(clientId, clientSecret) select {} }