image.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package process
  2. import (
  3. "fmt"
  4. "github.com/eryajf/chatgpt-dingtalk/public"
  5. "strings"
  6. "github.com/eryajf/chatgpt-dingtalk/pkg/db"
  7. "github.com/eryajf/chatgpt-dingtalk/pkg/dingbot"
  8. "github.com/eryajf/chatgpt-dingtalk/pkg/logger"
  9. "github.com/solywsh/chatgpt"
  10. )
  11. // ImageGenerate openai生成图片
  12. func ImageGenerate(rmsg *dingbot.ReceiveMsg) error {
  13. if public.Config.AzureOn {
  14. _, err := rmsg.ReplyToDingtalk(string(dingbot.
  15. MARKDOWN), "azure 模式下暂不支持图片创作功能")
  16. if err != nil {
  17. logger.Warning(fmt.Errorf("send message error: %v", err))
  18. }
  19. return err
  20. }
  21. qObj := db.Chat{
  22. Username: rmsg.SenderNick,
  23. Source: rmsg.GetChatTitle(),
  24. ChatType: db.Q,
  25. ParentContent: 0,
  26. Content: rmsg.Text.Content,
  27. }
  28. qid, err := qObj.Add()
  29. if err != nil {
  30. logger.Error("往MySQL新增数据失败,错误信息:", err)
  31. }
  32. reply, err := chatgpt.ImageQa(rmsg.Text.Content, rmsg.GetSenderIdentifier())
  33. if err != nil {
  34. logger.Info(fmt.Errorf("gpt request error: %v", err))
  35. _, err = rmsg.ReplyToDingtalk(string(dingbot.TEXT), fmt.Sprintf("请求openai失败了,错误信息:%v", err))
  36. if err != nil {
  37. logger.Error(fmt.Errorf("send message error: %v", err))
  38. return err
  39. }
  40. }
  41. if reply == "" {
  42. logger.Warning(fmt.Errorf("get gpt result falied: %v", err))
  43. return nil
  44. } else {
  45. reply = strings.TrimSpace(reply)
  46. reply = strings.Trim(reply, "\n")
  47. reply = fmt.Sprintf(">点击图片可旋转或放大。\n![](%s)", reply)
  48. aObj := db.Chat{
  49. Username: rmsg.SenderNick,
  50. Source: rmsg.GetChatTitle(),
  51. ChatType: db.A,
  52. ParentContent: qid,
  53. Content: reply,
  54. }
  55. _, err := aObj.Add()
  56. if err != nil {
  57. logger.Error("往MySQL新增数据失败,错误信息:", err)
  58. }
  59. logger.Info(fmt.Sprintf("🤖 %s得到的答案: %#v", rmsg.SenderNick, reply))
  60. // 回复@我的用户
  61. _, err = rmsg.ReplyToDingtalk(string(dingbot.MARKDOWN), reply)
  62. if err != nil {
  63. logger.Error(fmt.Errorf("send message error: %v", err))
  64. return err
  65. }
  66. }
  67. return nil
  68. }