mailchimp.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package mailchimp
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. "github.com/sirupsen/logrus"
  10. "github.com/spf13/viper"
  11. )
  12. type MailchimpAPI struct {
  13. APIKey string
  14. Server string
  15. AudienceID string
  16. }
  17. type Member struct {
  18. EmailAddress string `json:"email_address"`
  19. Status string `json:"status_if_new,omitempty"`
  20. }
  21. func NewMailchimpAPI() *MailchimpAPI {
  22. return &MailchimpAPI{
  23. APIKey: viper.GetString("mailchimp.api_key"),
  24. Server: viper.GetString("mailchimp.server"),
  25. AudienceID: viper.GetString("mailchimp.audience_id"),
  26. }
  27. }
  28. func (api *MailchimpAPI) emailHash(email string) string {
  29. hash := md5.Sum([]byte(strings.ToLower(email)))
  30. return hex.EncodeToString(hash[:])
  31. }
  32. func (api *MailchimpAPI) EmailExists(email string) (bool, error) {
  33. hash := api.emailHash(email)
  34. url := fmt.Sprintf("https://%s.api.mailchimp.com/3.0/lists/%s/members/%s",
  35. api.Server,
  36. api.AudienceID,
  37. hash)
  38. req, _ := http.NewRequest("GET", url, nil)
  39. req.SetBasicAuth("anystring", api.APIKey)
  40. resp, err := http.DefaultClient.Do(req)
  41. if err != nil {
  42. return false, err
  43. }
  44. defer resp.Body.Close()
  45. return resp.StatusCode == http.StatusOK, nil
  46. }
  47. func (api *MailchimpAPI) AddEmail(email string) error {
  48. url := fmt.Sprintf("https://%s.api.mailchimp.com/3.0/lists/%s/members",
  49. api.Server,
  50. api.AudienceID)
  51. member := Member{
  52. EmailAddress: email,
  53. Status: "subscribed",
  54. }
  55. body, _ := json.Marshal(member)
  56. req, _ := http.NewRequest("POST", url, strings.NewReader(string(body)))
  57. req.SetBasicAuth("anystring", api.APIKey)
  58. req.Header.Add("Content-Type", "application/json")
  59. resp, err := http.DefaultClient.Do(req)
  60. if err != nil {
  61. return err
  62. }
  63. defer resp.Body.Close()
  64. if resp.StatusCode >= 400 {
  65. return fmt.Errorf("failed to add email: %s", email)
  66. }
  67. return nil
  68. }
  69. func (api *MailchimpAPI) TagEmail(email, tag string) error {
  70. hash := api.emailHash(email)
  71. url := fmt.Sprintf("https://%s.api.mailchimp.com/3.0/lists/%s/members/%s/tags",
  72. api.Server,
  73. api.AudienceID,
  74. hash)
  75. body := map[string]interface{}{
  76. "tags": []map[string]string{
  77. {"name": tag, "status": "active"},
  78. },
  79. }
  80. jsonBody, _ := json.Marshal(body)
  81. req, _ := http.NewRequest("POST", url, strings.NewReader(string(jsonBody)))
  82. req.SetBasicAuth("anystring", api.APIKey)
  83. req.Header.Add("Content-Type", "application/json")
  84. resp, err := http.DefaultClient.Do(req)
  85. if err != nil {
  86. return err
  87. }
  88. defer resp.Body.Close()
  89. if resp.StatusCode >= 400 {
  90. logrus.Errorf("Tagging failed: status %d", resp.StatusCode)
  91. return fmt.Errorf("failed to tag email: %s", email)
  92. }
  93. return nil
  94. }