Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

173 lines
5.7KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "time"
  8. )
  9. type crmdLead struct {
  10. ID string `json:"id"`
  11. Name string `json:"name,omitempty"`
  12. Deleted bool `json:"deleted,omitempty"`
  13. SalutationName string `json:"salutationName,omitempty"`
  14. FirstName string `json:"firstName,omitempty"`
  15. LastName string `json:"lastName,omitempty"`
  16. Title string `json:"title,omitempty"`
  17. Status string `json:"status,omitempty"`
  18. Source string `json:"source,omitempty"`
  19. Industry string `json:"industry,omitempty"`
  20. OpportunityAmount int `json:"opportunityAmount,omitempty"`
  21. Website string `json:"website,omitempty"`
  22. AddressStreet string `json:"addressStreet,omitempty"`
  23. AddressCity string `json:"addressCity,omitempty"`
  24. AddressState string `json:"addressState,omitempty"`
  25. AddressCountry string `json:"addressCountry,omitempty"`
  26. AddressPostalCode string `json:"addresPostalCode,omitempty"`
  27. EmailAddress string `json:"emailAddress,omitempty"`
  28. PhoneNumber string `json:"phoneNumber,omitempty"`
  29. DoNotCall bool `json:"doNotCall,omitempty"`
  30. Description string `json:"description,omitempty"`
  31. CreatedAt string `json:"createdAt,omitempty"`
  32. ModifiedAt string `json:"ModifiedAt,omitempty"`
  33. AccountName string `json:"accountName,omitempty"`
  34. Password string `json:"password,omitempty"`
  35. WechatHitxyID string `json:"wechat_hitxy_id,omitempty"`
  36. Verifier []string `json:"verifier,omitempty"`
  37. OpportunityAmountCurrency string `json:"opportunityAmountCurrency,omitempty"`
  38. OpportunityAmountConverted int `json:"opportunityAmountConverted,omitempty"`
  39. EmailAddressData []struct {
  40. EmailAddress string `json:"emailAddress,omitempty"`
  41. Lower string `json:"lower,omitempty"`
  42. Primary bool `json:"primary,omitempty"`
  43. OptOut bool `json:"optOut,omitempty"`
  44. Invalid bool `json:"invalid,omitempty"`
  45. } `json:"emailAddressData,omitempty"`
  46. PhoneNumberData []struct {
  47. PhoneNumber string `json:"phoneNumber,omitempty"`
  48. Primary bool `json:"primary,omitempty"`
  49. Type string `json:"type,omitempty"`
  50. } `json:"phoneNumberData,omitempty"`
  51. CreatedByID string `json:"createdById,omitempty"`
  52. CreatedByName string `json:"createdByName,omitempty"`
  53. ModifiedByID string `json:"modifiedById,omitempty"`
  54. ModifiedByName string `json:"modifiedByName,omitempty"`
  55. AssignedUserID string `json:"assignedUserId,omitempty"`
  56. AssignedUserName string `json:"assignedUserName,omitempty"`
  57. CampaignID string `json:"campaignId,omitempty"`
  58. CreatedAccountID string `json:"createdAccountId,omitempty"`
  59. CreatedContactID string `json:"createdContactId,omitempty"`
  60. CreatedOpportunityID string `json:"createdOpportunityId,omitempty"`
  61. }
  62. type crmSearchLead struct {
  63. Total int `json:"total"`
  64. List []crmdLead `json:"list"`
  65. }
  66. func (m *crmdLead) getCreatedAt() (r time.Time) {
  67. layout := m.getTimeLayout()
  68. r, _ = time.Parse(layout, m.CreatedAt)
  69. return
  70. }
  71. func (m *crmdLead) setCreatedAt(v time.Time) string {
  72. layout := m.getTimeLayout()
  73. m.CreatedAt = v.Format(layout)
  74. return m.CreatedAt
  75. }
  76. func (m *crmdLead) getModifiedAt() (r time.Time) {
  77. layout := m.getTimeLayout()
  78. r, _ = time.Parse(layout, m.ModifiedAt)
  79. return
  80. }
  81. func (m *crmdLead) setModifiedAt(v time.Time) string {
  82. layout := m.getTimeLayout()
  83. m.ModifiedAt = v.Format(layout)
  84. return m.ModifiedAt
  85. }
  86. func (m *crmdLead) getTimeLayout() string {
  87. return "2006-01-02 15:04:05"
  88. }
  89. func crmFindOpenID(openID string) (info crmdLead, found bool, err error) {
  90. found = false
  91. req, err := http.NewRequest("GET", "https://c.hitxy.org.au/api/v1/Lead", nil)
  92. if err != nil {
  93. log.Println("crmGetRecord: cannot form good http Request")
  94. log.Print(err)
  95. return
  96. }
  97. //auth header
  98. req.Header.Set("Authorization", crmAuthHeader())
  99. req.Header.Set("Accept", "application/json, text/javascript, */*; q=0.01")
  100. req.Header.Set("Content-Type", "application/json")
  101. //query string
  102. q := req.URL.Query()
  103. q.Add("maxSize", "20")
  104. q.Add("maxSize", "0")
  105. q.Add("sortBy", "createdAt")
  106. q.Add("asc", "false")
  107. q.Add("where[0][type]", "equals")
  108. q.Add("where[0][attribute]", "wechat_hitxy_id")
  109. q.Add("where[0][value]", openID)
  110. req.URL.RawQuery = q.Encode()
  111. // dump, err := httputil.DumpRequest(req, true)
  112. // if err != nil {
  113. // log.Fatal(err)
  114. // }
  115. // fmt.Printf("dump : %q", dump)
  116. client := &http.Client{}
  117. r, err := client.Do(req)
  118. if err != nil {
  119. return
  120. }
  121. defer r.Body.Close()
  122. b, _ := ioutil.ReadAll(r.Body)
  123. search := crmSearchLead{}
  124. err = json.Unmarshal(b, &search)
  125. if err != nil || search.Total <= 0 {
  126. return
  127. }
  128. if search.Total > 1 {
  129. log.Printf("ERROR It's wrong to have multiple record for same wechat id %s", openID)
  130. for _, v := range search.List {
  131. log.Println(v)
  132. }
  133. return
  134. }
  135. //we successfully reach here
  136. info = search.List[0]
  137. found = true
  138. return
  139. }
  140. //crmPrepareAttachmentHTTPHeader when uploading a file, we need its mime, auth header, etc.
  141. func crmPrepareLeadUploadHTTPHeader() (headers map[string]string) {
  142. headers = map[string]string{}
  143. headers["Authorization"] = crmAuthHeader()
  144. headers["Accept"] = "application/json"
  145. headers["Content-Type"] = "application/json"
  146. return headers
  147. }
  148. func crmGetLeadByID(id string) (r crmdLead, err error) {
  149. resp, err := crmGetEntity("Lead", id)
  150. err = json.Unmarshal([]byte(resp), &r)
  151. return
  152. }