### Install and Import Dingrobot Source: https://github.com/royeo/dingrobot/blob/master/README.md Instructions for installing the package via go get and importing it into your Go project. ```shell go get -u github.com/royeo/dingrobot ``` ```go import "github.com/royeo/dingrobot" ``` -------------------------------- ### Install and Import Dingrobot Library Source: https://context7.com/royeo/dingrobot/llms.txt Instructions on how to install the dingrobot Go package using go get and how to import it into your Go projects. ```go // 安装 // go get -u github.com/royeo/dingrobot // 导入 import "github.com/royeo/dingrobot" ``` -------------------------------- ### Send Markdown Message via Dingrobot Source: https://github.com/royeo/dingrobot/blob/master/README.md Provides an example of sending formatted Markdown content, which supports images and links within the message body. ```go func main() { webhook := "https://oapi.dingtalk.com/robot/send?access_token=xxx" robot := dingrobot.NewRobot(webhook) title := "杭州天气" text := "#### 杭州天气 \n > 9度,@1825718XXXX 西北风1级,空气良89,相对温度73%\n\n > ![screenshot](http://i01.lw.aliimg.com/media/lALPBbCc1ZhJGIvNAkzNBLA_1200_588.png)\n > ###### 10点20分发布 [天气](http://www.thinkpage.cn/) " atMobiles := []string{"1825718XXXX"} isAtAll := false err := robot.SendMarkdown(title, text, atMobiles, isAtAll) if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Send ActionCard Message (SendActionCard) Source: https://context7.com/royeo/dingrobot/llms.txt Demonstrates sending ActionCard messages, which provide a card-like interface with richer display options. This includes custom button text, URLs, button orientation, and avatar visibility, suitable for guiding user actions. ```go package main import ( "log" "github.com/royeo/dingrobot" ) func main() { webhook := "https://oapi.dingtalk.com/robot/send?access_token=your_access_token" robot := dingrobot.NewRobot(webhook) title := "新版本发布通知" text := `![](https://example.com/release-banner.png) #### 应用 v2.0.0 正式发布 **更新内容:** - 全新的用户界面设计 - 性能优化,启动速度提升 50% - 新增深色模式支持 - 修复了若干已知问题 **发布时间:** 2024-01-15 14:00:00` singleTitle := "立即更新" singleURL := "https://app.example.com/download" btnOrientation := "0" // 0: 按钮竖直排列, 1: 按钮横向排列 hideAvatar := "0" // 0: 显示头像, 1: 隐藏头像 err := robot.SendActionCard(title, text, singleTitle, singleURL, btnOrientation, hideAvatar) if err != nil { log.Fatal("发送失败:", err) } // 隐藏头像的 ActionCard err = robot.SendActionCard( "审批通知", "#### 请假申请\n\n申请人: 张三\n请假类型: 年假\n请假时间: 2024-01-20 至 2024-01-22\n请假事由: 个人事务", "点击审批", "https://oa.example.com/approval/12345", "1", // 横向排列 "1", // 隐藏头像 ) if err != nil { log.Fatal("发送失败:", err) } } ``` -------------------------------- ### Send Text Message (SendText) Source: https://context7.com/royeo/dingrobot/llms.txt Provides examples of sending plain text messages to DingTalk groups using the SendText method. It covers sending basic text, mentioning specific users via mobile numbers, and mentioning everyone in the group. ```go package main import ( "log" "github.com/royeo/dingrobot" ) func main() { webhook := "https://oapi.dingtalk.com/robot/send?access_token=your_access_token" robot := dingrobot.NewRobot(webhook) // 发送简单文本消息 err := robot.SendText("服务器运行正常", nil, false) if err != nil { log.Fatal("发送失败:", err) } // 发送消息并 @ 指定成员 content := "紧急告警: CPU 使用率超过 90%,请 @1825718XXXX 处理" atMobiles := []string{"1825718XXXX", "1390000XXXX"} isAtAll := false err = robot.SendText(content, atMobiles, isAtAll) if err != nil { log.Fatal("发送失败:", err) } // 发送消息并 @ 所有人 err = robot.SendText("全员通知: 系统将于今晚 22:00 进行维护", nil, true) if err != nil { log.Fatal("发送失败:", err) } } ``` -------------------------------- ### Create Dingrobot Instance (NewRobot) Source: https://context7.com/royeo/dingrobot/llms.txt Demonstrates how to create a new Dingrobot instance by providing the Webhook URL obtained from the DingTalk group robot settings. The returned Roboter interface is used for sending messages. ```go package main import ( "log" "github.com/royeo/dingrobot" ) func main() { // 从钉钉群机器人设置中获取 Webhook 地址 webhook := "https://oapi.dingtalk.com/robot/send?access_token=your_access_token" // 创建机器人实例 robot := dingrobot.NewRobot(webhook) // 现在可以使用 robot 发送消息 log.Println("机器人创建成功") } ``` -------------------------------- ### Send Link Message (SendLink) Source: https://context7.com/royeo/dingrobot/llms.txt Illustrates how to send link messages, which include a title, description, a target URL, and an optional image URL. This is useful for sharing articles or directing users to detailed information pages. ```go package main import ( "log" "github.com/royeo/dingrobot" ) func main() { webhook := "https://oapi.dingtalk.com/robot/send?access_token=your_access_token" robot := dingrobot.NewRobot(webhook) title := "GitLab 代码合并通知" text := "用户 zhangsan 提交了合并请求 #123,请及时进行代码审查。分支: feature/user-auth -> master" messageURL := "https://gitlab.example.com/project/merge_requests/123" picURL := "https://gitlab.example.com/logo.png" err := robot.SendLink(title, text, messageURL, picURL) if err != nil { log.Fatal("发送失败:", err) } // 不带图片的链接消息 err = robot.SendLink( "Jenkins 构建完成", "项目 my-app 构建 #456 已完成,状态: 成功", "https://jenkins.example.com/job/my-app/456", "", // 图片 URL 为空 ) if err != nil { log.Fatal("发送失败:", err) } } ``` -------------------------------- ### Send Markdown Message (SendMarkdown) Source: https://context7.com/royeo/dingrobot/llms.txt Shows how to send messages formatted using Markdown, allowing for rich text elements like headings, quotes, bold text, links, and images. It also supports the @ mention functionality, suitable for structured reports and task lists. ```go package main import ( "log" "github.com/royeo/dingrobot" ) func main() { webhook := "https://oapi.dingtalk.com/robot/send?access_token=your_access_token" robot := dingrobot.NewRobot(webhook) title := "服务器监控报告" text := `#### 服务器监控报告 > **服务器**: prod-web-01 > **时间**: 2024-01-15 10:30:00 ##### 资源使用情况 - CPU: 45% - 内存: 68% - 磁盘: 72% ##### 服务状态 | 服务 | 状态 | |------|------| | nginx | 运行中 | | mysql | 运行中 | | redis | 运行中 | > ###### [查看详情](https://monitor.example.com/server/prod-web-01)` atMobiles := []string{"1825718XXXX"} isAtAll := false err := robot.SendMarkdown(title, text, atMobiles, isAtAll) if err != nil { log.Fatal("发送失败:", err) } // @ 所有人的 Markdown 消息 alertText := `#### 紧急告警 > 数据库连接池已满,请立即处理! - 当前连接数: 100/100 - 等待队列: 50` err = robot.SendMarkdown("紧急告警", alertText, nil, true) if err != nil { log.Fatal("发送失败:", err) } } ``` -------------------------------- ### Send Text Message via Dingrobot Source: https://github.com/royeo/dingrobot/blob/master/README.md Demonstrates how to initialize a robot and send a simple text message, including support for tagging specific mobile numbers. ```go func main() { webhook := "https://oapi.dingtalk.com/robot/send?access_token=xxx" robot := dingrobot.NewRobot(webhook) content := "我就是我, @1825718XXXX 是不一样的烟火" atMobiles := []string{"1825718XXXX"} isAtAll := false err := robot.SendText(content, atMobiles, isAtAll) if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Set Security Signature (SetSecret) Source: https://context7.com/royeo/dingrobot/llms.txt Explains how to set a security signature key for the Dingrobot instance. This enables the security signing mechanism, automatically adding timestamp and signature parameters to outgoing requests for enhanced security. ```go package main import ( "log" "github.com/royeo/dingrobot" ) func main() { webhook := "https://oapi.dingtalk.com/robot/send?access_token=your_access_token" robot := dingrobot.NewRobot(webhook) // 设置加签密钥(从钉钉机器人安全设置中获取) robot.SetSecret("SEC1234567890abcdef") // 发送消息时会自动添加签名 err := robot.SendText("这是一条带签名的消息", nil, false) if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Send Link Message via Dingrobot Source: https://github.com/royeo/dingrobot/blob/master/README.md Shows how to send a link-type message containing a title, text body, and a destination URL. ```go func main() { webhook := "https://oapi.dingtalk.com/robot/send?access_token=xxx" robot := dingrobot.NewRobot(webhook) title := "自定义机器人协议" text := "群机器人是钉钉群的高级扩展功能。" messageUrl := "https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.Rqyvqo&treeId=257&articleId=105735&docType=1" picUrl := "" err := robot.SendLink(title, text, messageUrl, picUrl) if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Send ActionCard Message via Dingrobot Source: https://github.com/royeo/dingrobot/blob/master/README.md Demonstrates sending an ActionCard message, which includes a title, body text, and a single call-to-action button. ```go func main() { webhook := "https://oapi.dingtalk.com/robot/send?access_token=xxx" robot := dingrobot.NewRobot(webhook) title := "乔布斯 20 年前想打造一间苹果咖啡厅" text := "![screenshot](@lADOpwk3K80C0M0FoA) \n #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化" singleTitle := "阅读全文" singleURL := "https://www.dingtalk.com/" btnOrientation := "0" hideAvatar := "0" err := robot.SendActionCard(title, text, singleTitle, singleURL, btnOrientation, hideAvatar) if err != nil { log.Fatal(err) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.