main.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "os"
  8. "strings"
  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. var Welcome string = `Commands:
  21. =================================
  22. 🙋 单聊 👉 单独聊天,缺省
  23. 🗣 串聊 👉 带上下文聊天
  24. 🔃 重置 👉 重置带上下文聊天
  25. 🚀 帮助 👉 显示帮助信息
  26. =================================
  27. 🚜 例:@我发送 空 或 帮助 将返回此帮助信息
  28. 💪 Power By https://github.com/eryajf/chatgpt-dingtalk
  29. `
  30. // 💵 余额 👉 查看接口可调用额度
  31. func Start() {
  32. // 定义一个处理器函数
  33. handler := func(w http.ResponseWriter, r *http.Request) {
  34. data, err := ioutil.ReadAll(r.Body)
  35. if err != nil {
  36. http.Error(w, err.Error(), http.StatusBadRequest)
  37. logger.Warning("read request body failed: %v\n", err.Error())
  38. return
  39. }
  40. if len(data) == 0 {
  41. logger.Warning("回调参数为空,以至于无法正常解析,请检查原因")
  42. return
  43. }
  44. var msgObj = new(public.ReceiveMsg)
  45. err = json.Unmarshal(data, &msgObj)
  46. if err != nil {
  47. logger.Warning("unmarshal request body failed: %v\n", err)
  48. }
  49. if msgObj.Text.Content == "" || msgObj.ChatbotUserID == "" {
  50. logger.Warning("从钉钉回调过来的内容为空,根据过往的经验,或许重新创建一下机器人,能解决这个问题")
  51. return
  52. }
  53. // TODO: 校验请求
  54. if len(msgObj.Text.Content) == 1 || msgObj.Text.Content == " 帮助" {
  55. // 欢迎信息
  56. msgObj.ReplyText(Welcome)
  57. } else {
  58. logger.Info(fmt.Sprintf("dingtalk callback parameters: %#v", msgObj))
  59. err = ProcessRequest(*msgObj)
  60. if err != nil {
  61. logger.Warning("process request failed: %v\n", err)
  62. }
  63. }
  64. }
  65. // 创建一个新的 HTTP 服务器
  66. server := &http.Server{
  67. Addr: ":8090",
  68. Handler: http.HandlerFunc(handler),
  69. }
  70. // 启动服务器
  71. logger.Info("Start Listen On ", server.Addr)
  72. err := server.ListenAndServe()
  73. if err != nil {
  74. logger.Danger(err)
  75. }
  76. }
  77. func FirstCheck(rmsg public.ReceiveMsg) bool {
  78. lc := UserService.GetUserMode(rmsg.SenderNick)
  79. if lc != "" && strings.Contains(lc, "串聊") {
  80. return true
  81. }
  82. return false
  83. }
  84. func ProcessRequest(rmsg public.ReceiveMsg) error {
  85. switch rmsg.Text.Content {
  86. case " 单聊":
  87. UserService.SetUserMode(rmsg.SenderNick, rmsg.Text.Content)
  88. rmsg.ReplyText(fmt.Sprintf("=====现在进入与👉%s👈单聊的模式 =====", rmsg.SenderNick))
  89. case " 串聊":
  90. UserService.SetUserMode(rmsg.SenderNick, rmsg.Text.Content)
  91. rmsg.ReplyText(fmt.Sprintf("=====现在进入与👉%s👈串聊的模式 =====", rmsg.SenderNick))
  92. case " 重置":
  93. UserService.ClearUserMode(rmsg.SenderNick)
  94. err := os.Remove("openaiCache/" + rmsg.SenderNick)
  95. if err != nil && !strings.Contains(fmt.Sprintf("%s", err), "no such file or directory") {
  96. rmsg.ReplyText(fmt.Sprintf("=====清理与👉%s👈的对话缓存失败,错误信息: %v\n请检查=====", rmsg.SenderNick, err))
  97. } else {
  98. rmsg.ReplyText(fmt.Sprintf("=====已重置与👉%s👈的对话模式,可以开始新的对话=====", rmsg.SenderNick))
  99. }
  100. default:
  101. if FirstCheck(rmsg) {
  102. cli, reply, err := public.ContextQa(rmsg.Text.Content, rmsg.SenderNick)
  103. if err != nil {
  104. logger.Info("gpt request error: %v \n", err)
  105. _, err = rmsg.ReplyText(fmt.Sprintf("请求openai失败了,错误信息:%v", err))
  106. if err != nil {
  107. logger.Warning("send message error: %v \n", err)
  108. return err
  109. }
  110. }
  111. if reply == "" {
  112. logger.Warning("get gpt result falied: %v\n", err)
  113. return nil
  114. } else {
  115. reply = strings.TrimSpace(reply)
  116. reply = strings.Trim(reply, "\n")
  117. // 回复@我的用户
  118. replyText := "@" + rmsg.SenderNick + "\n" + reply
  119. _, err = rmsg.ReplyText(replyText)
  120. if err != nil {
  121. logger.Warning("send message error: %v \n", err)
  122. return err
  123. }
  124. path := "openaiCache/" + rmsg.SenderNick
  125. cli.ChatContext.SaveConversation(path)
  126. }
  127. } else {
  128. reply, err := public.SingleQa(rmsg.Text.Content, rmsg.SenderNick)
  129. if err != nil {
  130. logger.Info("gpt request error: %v \n", err)
  131. _, err = rmsg.ReplyText(fmt.Sprintf("请求openai失败了,错误信息:%v", err))
  132. if err != nil {
  133. logger.Warning("send message error: %v \n", err)
  134. return err
  135. }
  136. }
  137. if reply == "" {
  138. logger.Warning("get gpt result falied: %v\n", err)
  139. return nil
  140. } else {
  141. reply = strings.TrimSpace(reply)
  142. reply = strings.Trim(reply, "\n")
  143. // 回复@我的用户
  144. replyText := "@" + rmsg.SenderNick + "\n" + reply
  145. _, err = rmsg.ReplyText(replyText)
  146. if err != nil {
  147. logger.Warning("send message error: %v \n", err)
  148. return err
  149. }
  150. }
  151. }
  152. }
  153. return nil
  154. }