analyze_contact_field.go 14 KB

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