image.go 2.0 KB

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