main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "github.com/eryajf/chatgpt-dingtalk/gtp"
  9. "github.com/eryajf/chatgpt-dingtalk/public"
  10. "github.com/eryajf/chatgpt-dingtalk/public/logger"
  11. "github.com/eryajf/chatgpt-dingtalk/service"
  12. )
  13. var UserService service.UserServiceInterface
  14. func init() {
  15. UserService = service.NewUserService()
  16. }
  17. func main() {
  18. Start()
  19. }
  20. func Start() {
  21. // 定义一个处理器函数
  22. handler := func(w http.ResponseWriter, r *http.Request) {
  23. data, err := ioutil.ReadAll(r.Body)
  24. if err != nil {
  25. http.Error(w, err.Error(), http.StatusBadRequest)
  26. logger.Warning("read request body failed: %v\n", err.Error())
  27. return
  28. }
  29. // TODO: 校验请求
  30. // fmt.Println(r.Header)
  31. if len(data) == 0 {
  32. logger.Warning("回调参数为空,以至于无法正常解析,请检查原因")
  33. return
  34. } else {
  35. var msgObj = new(public.ReceiveMsg)
  36. err = json.Unmarshal(data, &msgObj)
  37. if err != nil {
  38. logger.Warning("unmarshal request body failed: %v\n", err)
  39. }
  40. logger.Info(fmt.Sprintf("dingtalk callback parameters: %#v", msgObj))
  41. err = ProcessRequest(*msgObj)
  42. if err != nil {
  43. logger.Warning("process request failed: %v\n", err)
  44. }
  45. }
  46. }
  47. // 创建一个新的 HTTP 服务器
  48. server := &http.Server{
  49. Addr: ":8090",
  50. Handler: http.HandlerFunc(handler),
  51. }
  52. // 启动服务器
  53. logger.Info("Start Listen On ", server.Addr)
  54. err := server.ListenAndServe()
  55. if err != nil {
  56. logger.Danger(err)
  57. }
  58. }
  59. func ProcessRequest(rmsg public.ReceiveMsg) error {
  60. atText := "@" + rmsg.SenderNick + "\n" + " "
  61. if UserService.ClearUserSessionContext(rmsg.SenderID, rmsg.Text.Content) {
  62. _, err := rmsg.ReplyText(atText + "上下文已经清空了,你可以问下一个问题啦。")
  63. if err != nil {
  64. logger.Warning("response user error: %v \n", err)
  65. return err
  66. }
  67. } else {
  68. requestText := getRequestText(rmsg)
  69. // 获取问题的答案
  70. reply, err := gtp.Completions(requestText)
  71. if err != nil {
  72. logger.Info("gtp request error: %v \n", err)
  73. _, err = rmsg.ReplyText("机器人太累了,让她休息会儿,过一会儿再来请求。")
  74. if err != nil {
  75. logger.Warning("send message error: %v \n", err)
  76. return err
  77. }
  78. logger.Info("request openai error: %v\n", err)
  79. return err
  80. }
  81. if reply == "" {
  82. logger.Warning("get gpt result falied: %v\n", err)
  83. return nil
  84. }
  85. // 回复@我的用户
  86. reply = strings.TrimSpace(reply)
  87. reply = strings.Trim(reply, "\n")
  88. UserService.SetUserSessionContext(rmsg.SenderID, requestText, reply)
  89. replyText := atText + reply
  90. _, err = rmsg.ReplyText(replyText)
  91. if err != nil {
  92. logger.Info("send message error: %v \n", err)
  93. return err
  94. }
  95. }
  96. return nil
  97. }
  98. // getRequestText 获取请求接口的文本,要做一些清洗
  99. func getRequestText(rmsg public.ReceiveMsg) string {
  100. // 1.去除空格以及换行
  101. requestText := strings.TrimSpace(rmsg.Text.Content)
  102. requestText = strings.Trim(rmsg.Text.Content, "\n")
  103. // 2.替换掉当前用户名称
  104. replaceText := "@" + rmsg.SenderNick
  105. requestText = strings.TrimSpace(strings.ReplaceAll(rmsg.Text.Content, replaceText, ""))
  106. if requestText == "" {
  107. return ""
  108. }
  109. // 3.获取上下文,拼接在一起,如果字符长度超出4000,截取为4000。(GPT按字符长度算)
  110. requestText = UserService.GetUserSessionContext(rmsg.SenderID) + requestText
  111. if len(requestText) >= 4000 {
  112. requestText = requestText[:4000]
  113. }
  114. // 4.返回请求文本
  115. return requestText
  116. }