dingtalk.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package public
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. )
  7. // 接收的消息体
  8. type ReceiveMsg struct {
  9. ConversationID string `json:"conversationId"`
  10. AtUsers []struct {
  11. DingtalkID string `json:"dingtalkId"`
  12. } `json:"atUsers"`
  13. ChatbotUserID string `json:"chatbotUserId"`
  14. MsgID string `json:"msgId"`
  15. SenderNick string `json:"senderNick"`
  16. IsAdmin bool `json:"isAdmin"`
  17. SenderStaffId string `json:"senderStaffId"`
  18. SessionWebhookExpiredTime int64 `json:"sessionWebhookExpiredTime"`
  19. CreateAt int64 `json:"createAt"`
  20. ConversationType string `json:"conversationType"`
  21. SenderID string `json:"senderId"`
  22. ConversationTitle string `json:"conversationTitle"`
  23. IsInAtList bool `json:"isInAtList"`
  24. SessionWebhook string `json:"sessionWebhook"`
  25. Text Text `json:"text"`
  26. RobotCode string `json:"robotCode"`
  27. Msgtype MsgType `json:"msgtype"`
  28. }
  29. // 消息类型
  30. type MsgType string
  31. const TEXT MsgType = "text"
  32. const MARKDOWN MsgType = "markdown"
  33. // Text 消息
  34. type TextMessage struct {
  35. MsgType MsgType `json:"msgtype"`
  36. At *At `json:"at"`
  37. Text *Text `json:"text"`
  38. }
  39. // Text 消息内容
  40. type Text struct {
  41. Content string `json:"content"`
  42. }
  43. // MarkDown 消息
  44. type MarkDownMessage struct {
  45. MsgType MsgType `json:"msgtype"`
  46. At *At `json:"at"`
  47. MarkDown *MarkDown `json:"markdown"`
  48. }
  49. // MarkDown 消息内容
  50. type MarkDown struct {
  51. Title string `json:"title"`
  52. Text string `json:"text"`
  53. }
  54. // at 内容
  55. type At struct {
  56. AtUserIds []string `json:"atUserIds"`
  57. AtMobiles []string `json:"atMobiles"`
  58. IsAtAll bool `json:"isAtAll"`
  59. }
  60. // 发消息给钉钉
  61. func (r ReceiveMsg) ReplyToDingtalk(msgType, msg, staffId string) (statuscode int, err error) {
  62. var msgtmp interface{}
  63. switch msgType {
  64. case string(TEXT):
  65. msgtmp = &TextMessage{Text: &Text{Content: msg}, MsgType: TEXT, At: &At{AtUserIds: []string{staffId}}}
  66. case string(MARKDOWN):
  67. msgtmp = &MarkDownMessage{MsgType: MARKDOWN, At: &At{AtUserIds: []string{staffId}}, MarkDown: &MarkDown{Title: "根据您提供的信息,为您生成图片如下", Text: msg}}
  68. default:
  69. msgtmp = &TextMessage{Text: &Text{Content: msg}, MsgType: TEXT, At: &At{AtUserIds: []string{staffId}}}
  70. }
  71. data, err := json.Marshal(msgtmp)
  72. if err != nil {
  73. return 0, err
  74. }
  75. req, err := http.NewRequest("POST", r.SessionWebhook, bytes.NewBuffer(data))
  76. if err != nil {
  77. return 0, err
  78. }
  79. req.Header.Add("Accept", "*/*")
  80. req.Header.Add("Content-Type", "application/json")
  81. client := &http.Client{}
  82. resp, err := client.Do(req)
  83. if err != nil {
  84. return 0, err
  85. }
  86. defer resp.Body.Close()
  87. return resp.StatusCode, nil
  88. }