analyze_contact_field.go 14 KB

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