| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package robot
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "io"
- "net/http"
- )
- type DingTalkRobotMessage struct {
- ConversationID string `json:"conversationId"` // 会话 ID
- AtUsers []AtUser `json:"atUsers"` // @ 的人列表
- ChatbotCorpID string `json:"chatbotCorpId"` // 机器人企业 ID
- ChatbotUserID string `json:"chatbotUserId"` // 机器人用户 ID
- MsgID string `json:"msgId"` // 消息 ID
- SenderNick string `json:"senderNick"` // 发送者昵称
- IsAdmin bool `json:"isAdmin"` // 是否管理员
- SenderStaffID string `json:"senderStaffId"` // 发送者员工 ID
- SessionWebhookExpiredTime int64 `json:"sessionWebhookExpiredTime"` // 会话 Webhook 过期时间(毫秒时间戳)
- CreateAt int64 `json:"createAt"` // 创建时间(毫秒时间戳)
- SenderCorpID string `json:"senderCorpId"` // 发送者企业 ID
- ConversationType string `json:"conversationType"` // 会话类型(1: 单聊,2: 群聊)
- SenderID string `json:"senderId"` // 发送者 ID
- ConversationTitle string `json:"conversationTitle"` // 会话标题
- IsInAtList bool `json:"isInAtList"` // 是否在@列表中
- SessionWebhook string `json:"sessionWebhook"` // 会话 Webhook
- Text Text `json:"text"` // 消息
- MsgType string `json:"msgtype"` // 消息类型
- }
- // AtUser @的用户信息
- type AtUser struct {
- DingtalkID string `json:"dingtalkId"` // 钉钉 ID
- StaffID string `json:"staffId"` // 员工 ID
- }
- // 文本消息结构体
- type Text struct {
- Content string `json:"content"` // 消息内容
- }
- type Markdown struct {
- Title string `json:"title"`
- Text string `json:"text"`
- }
- // 钉钉回复消息的请求体结构体
- type DingReplyMsg struct {
- MsgType string `json:"msgtype"`
- Text Text `json:"text"`
- Markdown Markdown `json:"markdown"`
- }
- func SendReplyMsg(webhookUrl string, reply DingReplyMsg) error {
- // 序列化为 JSON
- jsonData, err := json.Marshal(reply)
- if err != nil {
- return fmt.Errorf("序列化消息失败:%v", err)
- }
- // 发送 POST 请求
- resp, err := http.Post(
- webhookUrl,
- "application/json",
- bytes.NewBuffer(jsonData),
- )
- if err != nil {
- return fmt.Errorf("发送请求失败:%v", err)
- }
- defer resp.Body.Close()
- // 读取响应结果
- respBody, _ := io.ReadAll(resp.Body)
- fmt.Printf("钉钉回复接口响应:%s\n", string(respBody))
- return nil
- }
|