dingbot.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // 发消息给钉钉
  62. func (r ReceiveMsg) ReplyToDingtalk(msgType, msg string) (statuscode int, err error) {
  63. atUser := r.SenderStaffId
  64. if atUser == "" {
  65. msg = fmt.Sprintf("%s\n\n@%s", msg, r.SenderNick)
  66. }
  67. var msgtmp interface{}
  68. switch msgType {
  69. case string(TEXT):
  70. msgtmp = &TextMessage{Text: &Text{Content: msg}, MsgType: TEXT, At: &At{AtUserIds: []string{atUser}}}
  71. case string(MARKDOWN):
  72. msgtmp = &MarkDownMessage{MsgType: MARKDOWN, At: &At{AtUserIds: []string{atUser}}, MarkDown: &MarkDown{Title: "Markdown Type", Text: msg}}
  73. default:
  74. msgtmp = &TextMessage{Text: &Text{Content: msg}, MsgType: TEXT, At: &At{AtUserIds: []string{atUser}}}
  75. }
  76. data, err := json.Marshal(msgtmp)
  77. if err != nil {
  78. return 0, err
  79. }
  80. req, err := http.NewRequest("POST", r.SessionWebhook, bytes.NewBuffer(data))
  81. if err != nil {
  82. return 0, err
  83. }
  84. req.Header.Add("Accept", "*/*")
  85. req.Header.Add("Content-Type", "application/json")
  86. client := &http.Client{}
  87. resp, err := client.Do(req)
  88. if err != nil {
  89. return 0, err
  90. }
  91. defer resp.Body.Close()
  92. return resp.StatusCode, nil
  93. }