# robot **Repository Path**: yebetter/robot ## Basic Information - **Project Name**: robot - **Description**: 机器人发送文字/图片消息 - **Primary Language**: Unknown - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-05-26 - **Last Updated**: 2026-04-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 获取组件包 ```cgo go get -u gitee.com/yebetter/robot ``` ### 使用方式 ``` /* 使用说明: 1. 钉钉(DingTalk): 仅支持 Webhook 模式。 2. 企业微信(Wechat): 支持 "webhook" (仅需URL) 和 "app" (需企业ID和应用Secret) 两种模式。 3. 飞书(Lark): 混合模式。发送走 Webhook URL,但上传图片/卡片必须配置 AppID 和 AppSecret。 */ // ============================================================================= // 1. 钉钉 (DingTalk) 测试用例 - 仅支持 Webhook // ============================================================================= func TestDingTalk_All(t *testing.T) { // 钉钉参数配置 params := map[string]string{ "url": "https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN", } bot := ditch.NewDingTalkBotFactory().InitFactory(params) // A. 文本测试 t.Run("Text", func(t *testing.T) { err := bot.SendMessage("【钉钉测试】这是一条生产级文本报警消息") handleResult("DingTalk Text", err) }) // B. 图片测试 (钉钉 Markdown 渲染) t.Run("Image", func(t *testing.T) { err := bot.SendImage("图片报警通知", cli.ImagePayload{ URL: "https://www.baidu.com/img/flexible/logo/pc/result.png", }) handleResult("DingTalk Image", err) }) // C. 图文链接测试 (Link 消息类型) t.Run("Link", func(t *testing.T) { err := bot.SendImageLink( "报警报表已生成", "点击查看本周详细业务增长数据", "https://www.baidu.com/img/flexible/logo/pc/result.png", "https://www.github.com", ) handleResult("DingTalk Link", err) }) } // ============================================================================= // 2. 企业微信 (Wechat) 测试用例 - 分 Webhook 和 App 模式 // ============================================================================= // Webhook 模式:简单、快速、权限受限 func TestWechat_Webhook(t *testing.T) { params := map[string]string{ "mode": "webhook", "url": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOUR_KEY", } bot := ditch.NewWechatBotFactory().InitFactory(params) // 企微 Webhook 图片仅支持 Base64 或 URL 渲染 Markdown err := bot.SendImage("企微Webhook图片", cli.ImagePayload{ URL: "https://www.baidu.com/img/flexible/logo/pc/result.png", }) handleResult("Wechat Webhook Image", err) } // App 模式:功能最强,支持上传素材、发送卡片 func TestWechat_App(t *testing.T) { params := map[string]string{ "mode": "app", // 必须指定为 app "corp_id": "ww12345678", // 企业ID "agent_id": "1000001", // 自建应用ID "agent_secret": "YOUR_SECRET", // 自建应用Secret } bot := ditch.NewWechatBotFactory().InitFactory(params) // A. 文本发送 t.Run("Text", func(t *testing.T) { err := bot.SendMessage("【企微应用测试】系统内存预警") handleResult("Wechat App Text", err) }) // B. 本地图片上传测试 (会先调用 API 上传获取 MediaID) t.Run("Image", func(t *testing.T) { err := bot.SendImage("素材上传测试", cli.ImagePayload{ LocalPath: "./test_report.png", // 确保本地存在此文件 }) handleResult("Wechat App Image Upload", err) }) // C. News 卡片测试 t.Run("Link", func(t *testing.T) { err := bot.SendImageLink( "关键指标异动", "昨日订单量下降 20%,请知悉", "https://www.baidu.com/img/flexible/logo/pc/result.png", "https://admin.yourcompany.com", ) handleResult("Wechat App News Card", err) }) } // ============================================================================= // 3. 飞书 (Lark) 测试用例 - 混合模式 // ============================================================================= func TestLark_Complete(t *testing.T) { /* 飞书特别注意: 1. 发送消息依赖 "url" (Webhook) 2. 发送图片(SendImage)依赖 "app_id" 和 "app_secret" 获取 Token 上传 */ params := map[string]string{ "url": "https://open.larksuite.com/open-apis/bot/v2/hook/YOUR_HOOK_ID", "app_id": "cli_a1b2c3d4e5", "app_secret": "YOUR_SECRET_KEY", } bot := ditch.NewLarkBotFactory().InitFactory(params) // A. 文本消息 t.Run("Text", func(t *testing.T) { err := bot.SendMessage("【飞书统计测试】这是一条通过自定义机器人发送的文本") handleResult("Lark Text", err) }) // B. 交互式卡片图片 (需本地文件) t.Run("Image", func(t *testing.T) { err := bot.SendImage("飞书统计交互式卡片-图片", cli.ImagePayload{ LocalPath: "../test.png", }) handleResult("Lark Image Card", err) }) // C. 交互式卡片按钮链接 t.Run("Link", func(t *testing.T) { err := bot.SendImageLink( "飞书统计卡片标题", "这是通过消息卡片发送的富文本描述", "", // 飞书该方法目前主要通过卡片展示描述和按钮 "https://open.feishu.cn", ) handleResult("Lark Link Card", err) }) } ```