sobot.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package sobot
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "go-common/library/log"
  10. "net/http"
  11. "net/url"
  12. "strconv"
  13. "strings"
  14. "go-common/app/service/main/workflow/model/sobot"
  15. )
  16. const (
  17. _sobotTicketModifyURL = "/ws/updateStatusBilibili/4"
  18. _sobotAddTicketURL = "/ws/addCustomerTicketBilibili/4"
  19. _sobotAddReplyURL = "/ws/addCustomerReplyInfoBilibili/4"
  20. _sobotTicketInfoURL = "/ws/queryTicketReplyByCustomerListBilibili/4"
  21. )
  22. // SobotTicketInfo get ticket into
  23. func (d *Dao) SobotTicketInfo(c context.Context, ticketID int32) (res json.RawMessage, err error) {
  24. var (
  25. req *http.Request
  26. )
  27. params := url.Values{}
  28. params.Set("companyId", d.c.HTTPClient.Sobot.Secret)
  29. params.Set("ticketId", strconv.Itoa(int(ticketID)))
  30. sign := md5.Sum([]byte(fmt.Sprintf("%s%s%d", d.c.HTTPClient.Sobot.Secret, d.c.HTTPClient.Sobot.Key, ticketID)))
  31. params.Set("sobotKey", hex.EncodeToString(sign[:]))
  32. if req, err = http.NewRequest("GET", d.ticketInfoURL+"?"+params.Encode(), nil); err != nil {
  33. log.Error("http.NewRequest(GET,%s) error(%v)", d.ticketInfoURL, err)
  34. return
  35. }
  36. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  37. if err = d.httpSobot.Do(c, req, &res); err != nil {
  38. log.Error("d.httpSobot.Do() error(%v)", err)
  39. return
  40. }
  41. return
  42. }
  43. // SobotAddTicket add ticket to sobot
  44. func (d *Dao) SobotAddTicket(c context.Context, tp *sobot.TicketParam) (err error) {
  45. var (
  46. req *http.Request
  47. res struct {
  48. RetCode string `json:"retCode"`
  49. Item string `json:"item"`
  50. }
  51. )
  52. params := url.Values{}
  53. params.Set("fileStr", tp.FileStr)
  54. params.Set("companyId", d.c.HTTPClient.Sobot.Secret)
  55. params.Set("customerName", tp.CustomerName)
  56. params.Set("customerQq", tp.CustomerQQ)
  57. params.Set("customerNick", tp.CustomerNick)
  58. params.Set("customerEmail", tp.CustomerEmail)
  59. params.Set("customerPhone", tp.CustomerPhone)
  60. params.Set("customerSource", strconv.Itoa(int(tp.CustomerSource)))
  61. params.Set("ticketId", strconv.Itoa(int(tp.TicketID)))
  62. params.Set("ticketTitle", tp.TicketTitle)
  63. params.Set("ticketContent", tp.TicketContent)
  64. params.Set("ticketLevel", strconv.Itoa(int(tp.TicketLevel)))
  65. params.Set("ticketStatus", strconv.Itoa(int(tp.TicketStatus)))
  66. params.Set("ticketFrom", strconv.Itoa(int(sobot.TicketFrom)))
  67. sign := md5.Sum([]byte(fmt.Sprintf("%s%s%d", d.c.HTTPClient.Sobot.Secret, d.c.HTTPClient.Sobot.Key, tp.TicketID)))
  68. params.Set("sobotKey", hex.EncodeToString(sign[:]))
  69. if req, err = http.NewRequest("POST", d.ticketAddURL, strings.NewReader(params.Encode())); err != nil {
  70. log.Error("http.NewRequest(POST,%s) error(%v)", d.ticketAddURL, err)
  71. return
  72. }
  73. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  74. if err = d.httpSobot.Do(c, req, &res); err != nil {
  75. log.Error("d.httpSobot.Do() error(%v)", err)
  76. return
  77. }
  78. if res.RetCode != sobot.EcodeOK {
  79. log.Error("d.httpSobot.Do() url(%s) ecode(%s)", d.ticketAddURL, res.RetCode)
  80. err = errors.New(res.RetCode)
  81. return
  82. }
  83. return
  84. }
  85. // SobotAddReply add reply to sobot
  86. func (d *Dao) SobotAddReply(c context.Context, rp *sobot.ReplyParam) (err error) {
  87. var (
  88. req *http.Request
  89. res struct {
  90. RetCode string `json:"retCode"`
  91. Item string `json:"item"`
  92. }
  93. )
  94. params := url.Values{}
  95. params.Set("companyId", d.c.HTTPClient.Sobot.Secret)
  96. params.Set("customerEmail", rp.CustomerEmail)
  97. params.Set("replyContent", rp.ReplyContent)
  98. params.Set("ticketId", strconv.Itoa(int(rp.TicketID)))
  99. params.Set("replyType", strconv.Itoa(int(rp.ReplyType)))
  100. params.Set("startType", strconv.Itoa(int(rp.StartType)))
  101. sign := md5.Sum([]byte(fmt.Sprintf("%s%s%d", d.c.HTTPClient.Sobot.Secret, d.c.HTTPClient.Sobot.Key, rp.TicketID)))
  102. params.Set("sobotKey", hex.EncodeToString(sign[:]))
  103. if req, err = http.NewRequest("POST", d.replyAddURL, strings.NewReader(params.Encode())); err != nil {
  104. log.Error("http.NewRequest(POST,%s) error(%v)", d.replyAddURL, err)
  105. return
  106. }
  107. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  108. if err = d.httpSobot.Do(c, req, &res); err != nil {
  109. log.Error("d.httpSobot.Do() error(%v)", err)
  110. return
  111. }
  112. if res.RetCode != sobot.EcodeOK {
  113. log.Error("d.httpSobot.Do() url(%s) ecode(%s)", d.replyAddURL, res.RetCode)
  114. err = errors.New(res.RetCode)
  115. return
  116. }
  117. return
  118. }
  119. // SobotTicketModify modify ticket
  120. func (d *Dao) SobotTicketModify(c context.Context, tp *sobot.TicketParam) (err error) {
  121. // http
  122. var (
  123. req *http.Request
  124. res struct {
  125. RetCode string `json:"retCode"`
  126. Item string `json:"item"`
  127. }
  128. )
  129. params := url.Values{}
  130. params.Set("companyId", d.c.HTTPClient.Sobot.Secret)
  131. params.Set("customerEmail", tp.CustomerEmail)
  132. params.Set("ticketId", strconv.Itoa(int(tp.TicketID)))
  133. params.Set("ticketFrom", strconv.Itoa(int(sobot.TicketFrom)))
  134. params.Set("ticketStatus", strconv.Itoa(int(tp.TicketStatus)))
  135. params.Set("startType", strconv.Itoa(int(tp.StartType)))
  136. sign := md5.Sum([]byte(fmt.Sprintf("%s%s%d", d.c.HTTPClient.Sobot.Secret, d.c.HTTPClient.Sobot.Key, tp.TicketID)))
  137. params.Set("sobotKey", hex.EncodeToString(sign[:]))
  138. if req, err = http.NewRequest("POST", d.ticketModifyURL, strings.NewReader(params.Encode())); err != nil {
  139. log.Error("http.NewRequest(POST,%s) error(%v)", d.ticketModifyURL, err)
  140. return
  141. }
  142. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  143. if err = d.httpSobot.Do(c, req, &res); err != nil {
  144. log.Error("d.httpSobot.Do() error(%v)", err)
  145. return
  146. }
  147. if res.RetCode != sobot.EcodeOK {
  148. log.Error("d.httpSobot.Do() url(%s) ecode(%s)", d.ticketModifyURL, res.RetCode)
  149. err = errors.New(res.RetCode)
  150. return
  151. }
  152. return
  153. }