main.go 3.4 KB

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