contact_form.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package crontask
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/google/uuid"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "strconv"
  8. "time"
  9. "wechat-api/ent/contact"
  10. "wechat-api/ent/contactfield"
  11. "wechat-api/ent/contactfieldtemplate"
  12. "wechat-api/ent/custom_types"
  13. "wechat-api/ent/predicate"
  14. "wechat-api/ent/usagedetail"
  15. "wechat-api/internal/types"
  16. "wechat-api/internal/utils/compapi"
  17. )
  18. type ResponseItem struct {
  19. DataIndex string `json:"dataIndex"`
  20. Value []string `json:"value"`
  21. }
  22. type FieldPropsOptions struct {
  23. Label string `json:"label"`
  24. Value string `json:"value"`
  25. }
  26. type FieldProps struct {
  27. Options []FieldPropsOptions `json:"options"`
  28. }
  29. type FormData struct {
  30. Title string `json:"title"`
  31. DataIndex string `json:"dataIndex"`
  32. ValueType string `json:"valueType"`
  33. FieldProps FieldProps `json:"fieldProps"`
  34. }
  35. func (l *CronTask) analyze() {
  36. usageDetails := make(map[string]map[string]string)
  37. contactFieldTemplates := make(map[string][]custom_types.ContactFieldTemplate)
  38. template_type_text := "text"
  39. template_type_radio := "radio"
  40. //template_type_date := "date"
  41. template_sex_id := "sex"
  42. template_sex_label := "性别"
  43. template_sex_options_man_label := "男"
  44. template_sex_options_man_value := "男"
  45. template_sex_options_woman_label := "女"
  46. template_sex_options_woman_value := "男"
  47. template_phone_id := "phone"
  48. template_phone_label := "手机号"
  49. //template_name_id := "name"
  50. //template_name_label := "姓名"
  51. //
  52. //template_age_id := "age"
  53. //template_age_label := "年龄(以字符串形式返回阿拉伯数字)"
  54. //
  55. //template_area_id := "area"
  56. //template_area_label := "地区"
  57. //
  58. //template_birthday_id := "birthday"
  59. //template_birthday_label := "出生日期"
  60. //
  61. //template_birtharea_id := "birtharea"
  62. //template_birtharea_label := "出生地"
  63. //
  64. //template_idcard_no_id := "idcard_no"
  65. //template_idcard_no_label := "身份证号"
  66. //
  67. //template_title_id := "title"
  68. //template_title_label := "称呼"
  69. contactBasicFieldTemplates := []custom_types.ContactFieldTemplate{
  70. {
  71. Label: &template_sex_label,
  72. Id: &template_sex_id,
  73. Type: &template_type_radio,
  74. Options: []custom_types.ContactFieldTemplateOptions{
  75. {
  76. Label: &template_sex_options_man_label,
  77. Value: &template_sex_options_man_value,
  78. }, {
  79. Label: &template_sex_options_woman_label,
  80. Value: &template_sex_options_woman_value,
  81. },
  82. },
  83. },
  84. {
  85. Label: &template_phone_label,
  86. Id: &template_phone_id,
  87. Type: &template_type_text,
  88. },
  89. //{
  90. // Label: &template_name_label,
  91. // Id: &template_name_id,
  92. // Type: &template_type_text,
  93. //}, {
  94. // Label: &template_age_label,
  95. // Id: &template_age_id,
  96. // Type: &template_type_text,
  97. //}, {
  98. // Label: &template_area_label,
  99. // Id: &template_area_id,
  100. // Type: &template_type_text,
  101. //}, {
  102. // Label: &template_birthday_label,
  103. // Id: &template_birthday_id,
  104. // Type: &template_type_date,
  105. //}, {
  106. // Label: &template_birtharea_label,
  107. // Id: &template_birtharea_id,
  108. // Type: &template_type_text,
  109. //}, {
  110. // Label: &template_idcard_no_label,
  111. // Id: &template_idcard_no_id,
  112. // Type: &template_type_text,
  113. //}, {
  114. // Label: &template_title_label,
  115. // Id: &template_title_id,
  116. // Type: &template_type_text,
  117. //},
  118. }
  119. var predicates []predicate.UsageDetail
  120. predicates = append(predicates, usagedetail.TypeIn(1, 3, 4, 6))
  121. predicates = append(predicates, usagedetail.AppIn(1, 3, 4, 5))
  122. //yesterdayStart := time.Now().AddDate(0, 0, -1).Truncate(24 * time.Hour)
  123. //yesterdayEnd := yesterdayStart.Add(24 * time.Hour)
  124. //yesterdayEnd := time.Now().Truncate(24 * time.Hour)
  125. //yesterdayStart := yesterdayEnd.AddDate(0, 0, -1)
  126. //predicates = append(predicates, usagedetail.CreatedAtGTE(yesterdayStart))
  127. //predicates = append(predicates, usagedetail.CreatedAtLT(yesterdayEnd))
  128. //logx.Info("yesterdayEnd: ", yesterdayEnd)
  129. //logx.Info("yesterdayStart: ", yesterdayStart)
  130. todayStart := time.Now().AddDate(0, 0, 0).Truncate(24 * time.Hour)
  131. todayEnd := todayStart.Add(24 * time.Hour)
  132. predicates = append(predicates, usagedetail.CreatedAtGTE(todayStart))
  133. predicates = append(predicates, usagedetail.CreatedAtLT(todayEnd))
  134. logx.Info("todayStart: ", todayStart)
  135. logx.Info("todayEnd: ", todayEnd)
  136. data, err := l.svcCtx.DB.UsageDetail.Query().Where(predicates...).All(l.ctx)
  137. logx.Info("usageDetails: ", data)
  138. if err != nil {
  139. return
  140. }
  141. for _, u := range data {
  142. if _, ok := contactFieldTemplates[u.BotID]; !ok {
  143. c, _ := l.svcCtx.DB.ContactFieldTemplate.Query().Where(contactfieldtemplate.OrganizationID(u.OrganizationID)).First(l.ctx)
  144. if c != nil {
  145. contactFieldTemplates[u.BotID] = c.Template
  146. } else {
  147. contactFieldTemplates[u.BotID] = nil
  148. }
  149. }
  150. if contactFieldTemplates[u.BotID] == nil {
  151. continue
  152. }
  153. if _, ok := usageDetails[u.BotID]; !ok {
  154. usageDetails[u.BotID] = make(map[string]string)
  155. }
  156. usageDetails[u.BotID][u.ReceiverID] += fmt.Sprintf("用户:%s\n机器人:%s\n", u.Request, u.Response)
  157. }
  158. logx.Info("contactFieldTemplates: ", contactFieldTemplates)
  159. logx.Info("usageDetails: ", usageDetails)
  160. for botID, template := range contactFieldTemplates {
  161. if template == nil {
  162. template = contactBasicFieldTemplates
  163. } else {
  164. template = append(template, contactBasicFieldTemplates...)
  165. }
  166. for receiverID, messages := range usageDetails[botID] {
  167. result, _ := l.openaiRequest(messages, template)
  168. logx.Info("result: ", result)
  169. if result == nil {
  170. continue
  171. }
  172. _ = l.UpdateContactFields(botID, receiverID, result)
  173. }
  174. }
  175. }
  176. func (l *CronTask) openaiRequest(messages string, template []custom_types.ContactFieldTemplate) ([]ResponseItem, error) {
  177. formData := ConvertFormData(template)
  178. jsonBytes, err := json.Marshal(formData)
  179. if err != nil {
  180. return nil, err
  181. }
  182. jsonStr := string(jsonBytes)
  183. req := &types.CompApiReq{
  184. types.CompCtlReq{
  185. "form",
  186. "",
  187. false,
  188. "",
  189. },
  190. types.StdCompApiReq{
  191. "gpt-4o",
  192. []types.StdCompMessage{},
  193. false,
  194. nil,
  195. },
  196. types.FastGptSpecReq{
  197. "",
  198. "",
  199. "",
  200. false,
  201. map[string]string{
  202. "form_data": jsonStr,
  203. "chat_history": messages,
  204. "external_id": uuid.New().String(),
  205. },
  206. },
  207. }
  208. resp, err := compapi.NewClient(l.ctx, compapi.WithApiBase("http://new-api.gkscrm.com/v1/"),
  209. compapi.WithApiKey("sk-wwttAtdLcTfeF7F2Eb9d3592Bd4c487f8e8fA544D6C4BbA9")).
  210. Chat(req)
  211. logx.Info("resp: ", resp)
  212. if err == nil && resp != nil && len(resp.Choices) > 0 {
  213. logx.Info("resp.Choices: ", resp.Choices[0].Message.Content)
  214. // 尝试第一层解析成 string
  215. var inner string
  216. if err := json.Unmarshal([]byte(resp.Choices[0].Message.Content), &inner); err == nil {
  217. // 成功表示 data 是个被编码过的 JSON 字符串
  218. resp.Choices[0].Message.Content = inner
  219. }
  220. // 解析最终目标
  221. var items []ResponseItem
  222. err := json.Unmarshal([]byte(resp.Choices[0].Message.Content), &items)
  223. if err != nil {
  224. return nil, err
  225. }
  226. //var items []ResponseItem
  227. //err = json.Unmarshal([]byte(resp.Choices[0].Message.Content), &items)
  228. //if err != nil {
  229. // return nil, err
  230. //}
  231. return items, nil
  232. } else if resp != nil && len(resp.Choices) == 0 {
  233. return nil, err
  234. }
  235. //url := "https://toolsapi-debug.gkscrm.com/call_center/form/extract"
  236. //bodyData := map[string]interface{}{
  237. // "form_data": ConvertFormData(template),
  238. // "chat_history": messages,
  239. // "external_id": uuid.New().String(),
  240. //}
  241. //logx.Info("bodyData: %+v", bodyData)
  242. //bodyBytes, err := json.Marshal(bodyData)
  243. //if err != nil {
  244. // return nil, err
  245. //}
  246. //
  247. //req, err := http.NewRequest("POST", url, bytes.NewBuffer(bodyBytes))
  248. //if err != nil {
  249. // return nil, err
  250. //}
  251. //req.Header.Set("Content-Type", "application/json")
  252. //req.Header.Set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIn0.ZS9jnsLPCnmc8L_lu4yaQFp34vwWF1mHlHSBYrY5JVs")
  253. //
  254. //client := &http.Client{}
  255. //resp, err := client.Do(req)
  256. //if err != nil || resp == nil || resp.Body == nil {
  257. // logx.Error("read body error: ", err)
  258. // return nil, err
  259. //}
  260. //
  261. //logx.Info("err: ", err)
  262. //if err != nil {
  263. // return nil, err
  264. //}
  265. //defer resp.Body.Close()
  266. //
  267. //if resp.StatusCode != http.StatusOK {
  268. // return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
  269. //}
  270. //
  271. ////var result []ResponseItem
  272. //var fullResp struct {
  273. // Data []ResponseItem `json:"data"`
  274. //}
  275. //err = json.NewDecoder(resp.Body).Decode(&fullResp)
  276. //if err != nil {
  277. // return nil, err
  278. //}
  279. //
  280. //return fullResp.Data, nil
  281. return nil, err
  282. }
  283. func (l *CronTask) UpdateContactFields(botID string, receiverID string, fields []ResponseItem) error {
  284. basic_ids := []string{"sex", "phone", "name", "age", "area", "birthday", "birtharea", "idcard_no", "title"}
  285. c, _ := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(botID), contact.WxidEQ(receiverID)).First(l.ctx)
  286. if c == nil {
  287. return fmt.Errorf("Contact not find")
  288. }
  289. for _, field := range fields {
  290. if contains(basic_ids, field.DataIndex) {
  291. if len(field.Value) == 0 {
  292. continue
  293. }
  294. value := 0
  295. if field.DataIndex == "sex" && c.Sex == 0 {
  296. if field.Value[0] == "男" {
  297. value = 1
  298. } else if field.Value[0] == "女" {
  299. value = 2
  300. }
  301. _, err := l.svcCtx.DB.Contact.Update().
  302. Where(contact.WxidEQ(receiverID)).
  303. SetSex(value).
  304. Save(l.ctx)
  305. if err != nil {
  306. continue
  307. }
  308. } else if field.DataIndex == "phone" && c.Phone == "" {
  309. _, err := l.svcCtx.DB.Contact.Update().
  310. Where(contact.WxidEQ(receiverID)).
  311. SetPhone(field.Value[0]).
  312. Save(l.ctx)
  313. if err != nil {
  314. continue
  315. }
  316. } else if field.DataIndex == "name" && c.Cname == "" {
  317. _, err := l.svcCtx.DB.Contact.Update().
  318. Where(contact.WxidEQ(receiverID)).
  319. SetCname(field.Value[0]).
  320. Save(l.ctx)
  321. if err != nil {
  322. continue
  323. }
  324. } else if field.DataIndex == "age" && c.Cage == 0 {
  325. num, err := strconv.Atoi(field.Value[0])
  326. if err != nil {
  327. continue
  328. }
  329. _, err = l.svcCtx.DB.Contact.Update().
  330. Where(contact.WxidEQ(receiverID)).
  331. SetCage(num).
  332. Save(l.ctx)
  333. if err != nil {
  334. continue
  335. }
  336. } else if field.DataIndex == "area" && c.Carea == "" {
  337. _, err := l.svcCtx.DB.Contact.Update().
  338. Where(contact.WxidEQ(receiverID)).
  339. SetCarea(field.Value[0]).
  340. Save(l.ctx)
  341. if err != nil {
  342. continue
  343. }
  344. } else if field.DataIndex == "birthday" && c.Cbirthday == "" {
  345. _, err := l.svcCtx.DB.Contact.Update().
  346. Where(contact.WxidEQ(receiverID)).
  347. SetCbirthday(field.Value[0]).
  348. Save(l.ctx)
  349. if err != nil {
  350. continue
  351. }
  352. } else if field.DataIndex == "birtharea" && c.Cbirtharea == "" {
  353. _, err := l.svcCtx.DB.Contact.Update().
  354. Where(contact.WxidEQ(receiverID)).
  355. SetCbirtharea(field.Value[0]).
  356. Save(l.ctx)
  357. if err != nil {
  358. continue
  359. }
  360. } else if field.DataIndex == "idcard_no" && c.CidcardNo == "" {
  361. _, err := l.svcCtx.DB.Contact.Update().
  362. Where(contact.WxidEQ(receiverID)).
  363. SetCidcardNo(field.Value[0]).
  364. Save(l.ctx)
  365. if err != nil {
  366. continue
  367. }
  368. } else if field.DataIndex == "title" && c.Ctitle == "" {
  369. _, err := l.svcCtx.DB.Contact.Update().
  370. Where(contact.WxidEQ(receiverID)).
  371. SetCtitle(field.Value[0]).
  372. Save(l.ctx)
  373. if err != nil {
  374. continue
  375. }
  376. }
  377. } else {
  378. f, _ := l.svcCtx.DB.ContactField.Query().Where(contactfield.ContactID(c.ID), contactfield.FormID(field.DataIndex)).First(l.ctx)
  379. if f == nil {
  380. if field.Value != nil && len(field.Value) > 0 && field.Value[0] != "" {
  381. _, err := l.svcCtx.DB.ContactField.Create().
  382. SetContactID(c.ID).
  383. SetFormID(field.DataIndex).
  384. SetValue(field.Value).
  385. Save(l.ctx)
  386. if err != nil {
  387. continue
  388. }
  389. }
  390. } else {
  391. if field.Value != nil {
  392. if len(field.Value) == 0 || field.Value[0] == "" {
  393. continue
  394. }
  395. _, err := l.svcCtx.DB.ContactField.UpdateOneID(f.ID).
  396. SetValue(field.Value).
  397. Save(l.ctx)
  398. if err != nil {
  399. continue
  400. }
  401. }
  402. }
  403. }
  404. }
  405. return nil
  406. }
  407. func ConvertFormData(input []custom_types.ContactFieldTemplate) []FormData {
  408. result := make([]FormData, len(input))
  409. for i, item := range input {
  410. options := make([]FieldPropsOptions, len(item.Options))
  411. for j, opt := range item.Options {
  412. options[j] = FieldPropsOptions{
  413. Label: *opt.Label,
  414. Value: *opt.Value,
  415. }
  416. }
  417. result[i] = FormData{
  418. Title: *item.Label,
  419. DataIndex: *item.Id,
  420. ValueType: *item.Type,
  421. FieldProps: FieldProps{
  422. Options: options,
  423. },
  424. }
  425. }
  426. return result
  427. }
  428. func contains(strs []string, target string) bool {
  429. for _, s := range strs {
  430. if s == target {
  431. return true
  432. }
  433. }
  434. return false
  435. }