dingbot.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package dingbot
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. )
  8. // 接收的消息体
  9. type ReceiveMsg struct {
  10. ConversationID string `json:"conversationId"`
  11. AtUsers []struct {
  12. DingtalkID string `json:"dingtalkId"`
  13. } `json:"atUsers"`
  14. ChatbotUserID string `json:"chatbotUserId"`
  15. MsgID string `json:"msgId"`
  16. SenderNick string `json:"senderNick"`
  17. IsAdmin bool `json:"isAdmin"`
  18. SenderStaffId string `json:"senderStaffId"`
  19. SessionWebhookExpiredTime int64 `json:"sessionWebhookExpiredTime"`
  20. CreateAt int64 `json:"createAt"`
  21. ConversationType string `json:"conversationType"`
  22. SenderID string `json:"senderId"`
  23. ConversationTitle string `json:"conversationTitle"`
  24. IsInAtList bool `json:"isInAtList"`
  25. SessionWebhook string `json:"sessionWebhook"`
  26. Text Text `json:"text"`
  27. RobotCode string `json:"robotCode"`
  28. Msgtype MsgType `json:"msgtype"`
  29. }
  30. // 消息类型
  31. type MsgType string
  32. const TEXT MsgType = "text"
  33. const MARKDOWN MsgType = "markdown"
  34. // Text 消息
  35. type TextMessage struct {
  36. MsgType MsgType `json:"msgtype"`
  37. At *At `json:"at"`
  38. Text *Text `json:"text"`
  39. }
  40. // Text 消息内容
  41. type Text struct {
  42. Content string `json:"content"`
  43. }
  44. // MarkDown 消息
  45. type MarkDownMessage struct {
  46. MsgType MsgType `json:"msgtype"`
  47. At *At `json:"at"`
  48. MarkDown *MarkDown `json:"markdown"`
  49. }
  50. // MarkDown 消息内容
  51. type MarkDown struct {
  52. Title string `json:"title"`
  53. Text string `json:"text"`
  54. }
  55. // at 内容
  56. type At struct {
  57. AtUserIds []string `json:"atUserIds"`
  58. AtMobiles []string `json:"atMobiles"`
  59. IsAtAll bool `json:"isAtAll"`
  60. }
  61. // 获取用户标识,兼容当 SenderStaffId 字段为空的场景,此处提供给发送消息是艾特使用
  62. func (r ReceiveMsg) GetSenderIdentifier() (uid string) {
  63. uid = r.SenderStaffId
  64. if uid == "" {
  65. uid = r.SenderNick
  66. }
  67. return
  68. }
  69. // GetChatTitle 获取聊天的群名字,如果是私聊,则命名为 昵称_私聊
  70. func (r ReceiveMsg) GetChatTitle() (chatType string) {
  71. chatType = r.ConversationTitle
  72. if chatType == "" {
  73. chatType = r.SenderNick + "_私聊"
  74. }
  75. return
  76. }
  77. // 发消息给钉钉
  78. func (r ReceiveMsg) ReplyToDingtalk(msgType, msg string) (statuscode int, err error) {
  79. atUser := r.SenderStaffId
  80. if atUser == "" {
  81. msg = fmt.Sprintf("%s\n\n@%s", msg, r.SenderNick)
  82. }
  83. var msgtmp interface{}
  84. switch msgType {
  85. case string(TEXT):
  86. msgtmp = &TextMessage{Text: &Text{Content: msg}, MsgType: TEXT, At: &At{AtUserIds: []string{atUser}}}
  87. case string(MARKDOWN):
  88. msgtmp = &MarkDownMessage{MsgType: MARKDOWN, At: &At{AtUserIds: []string{atUser}}, MarkDown: &MarkDown{Title: "Markdown Type", Text: msg}}
  89. default:
  90. msgtmp = &TextMessage{Text: &Text{Content: msg}, MsgType: TEXT, At: &At{AtUserIds: []string{atUser}}}
  91. }
  92. data, err := json.Marshal(msgtmp)
  93. if err != nil {
  94. return 0, err
  95. }
  96. req, err := http.NewRequest("POST", r.SessionWebhook, bytes.NewBuffer(data))
  97. if err != nil {
  98. return 0, err
  99. }
  100. req.Header.Add("Accept", "*/*")
  101. req.Header.Add("Content-Type", "application/json")
  102. client := &http.Client{}
  103. resp, err := client.Do(req)
  104. if err != nil {
  105. return 0, err
  106. }
  107. defer resp.Body.Close()
  108. return resp.StatusCode, nil
  109. }