Skip to content

Instantly share code, notes, and snippets.

@HokieGeek
Last active April 6, 2022 17:41
Show Gist options
  • Save HokieGeek/ef659bed4f3d6244e2b0f5bb46413a79 to your computer and use it in GitHub Desktop.
Save HokieGeek/ef659bed4f3d6244e2b0f5bb46413a79 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
)
const baseURL = "https://api.influitive.com"
var (
influitiveToken, influitiveOrgID string
)
type Member struct {
ID int64 `json:"id"`
Name string `json:"name"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
Title string `json:"title"`
Company string `json:"company"`
UUID string `json:"uuid"`
Type string `json:"type"`
CreatedAt string `json:"created_at"`
JoinedAt string `json:"joined_at"`
LockedAt string `json:"locked_at"`
ExternalIDS map[string]string `json:"external_ids"`
MatchCategories map[string]string `json:"match_categories"`
CustomFields map[string]string `json:"custom_fields"`
NpsScore int64 `json:"nps_score"`
CurrentPoints int64 `json:"current_points"`
LifetimePoints int64 `json:"lifetime_points"`
CRMContactID string `json:"crm_contact_id"`
SalesforceID string `json:"salesforce_id"`
InviteLink string `json:"invite_link"`
Language string `json:"language"`
Address string `json:"address"`
Level Level `json:"level"`
Source string `json:"source"`
Thumb string `json:"thumb"`
}
type Level struct {
ID int64 `json:"id"`
Name string `json:"name"`
}
// https://influitive.readme.io/reference#get-details-about-a-member-identified-by-email
func getMemberByEmail(email string) (Member, error) {
qp := url.Values{}
qp.Set("email", email)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/members?%s", baseURL, qp.Encode()), nil)
if err != nil {
return Member{}, err
}
req.Header.Set("Authorization", fmt.Sprintf("Token %s", influitiveToken))
req.Header.Set("X_ORG_ID", influitiveOrgID)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return Member{}, fmt.Errorf("unable to retrieve details of member by email: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if body, err := ioutil.ReadAll(resp.Body); err == nil {
fmt.Println(string(body))
}
return Member{}, fmt.Errorf("influitive did not return good status: %s", resp.Status)
}
var members []Member
if err := json.NewDecoder(resp.Body).Decode(&members); err != nil {
return Member{}, fmt.Errorf("unable to read message body as member details: %v", err)
}
if len(members) > 1 {
return Member{}, errors.New("found more than 1 member with given email address")
} else if len(members) == 0 {
return Member{}, errors.New("did not find a member with the given email address")
}
return members[0], nil
}
type eventMember struct {
ID int64 `json:"id"`
Email string `json:"email"`
CRMContactID string `json:"crm_contact_id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
type logEventRequest struct {
Type string `json:"type"`
Member eventMember `json:"member"`
Notes string `json:"notes"`
Link string `json:"link"`
Points int64 `json:"points"`
}
// https://influitive.readme.io/reference#post-reference-type-events
func logEvent(eventType string, member Member) error {
evReq := logEventRequest{
Type: eventType,
Member: eventMember{
ID: member.ID,
Email: member.Email,
CRMContactID: member.CRMContactID,
FirstName: member.FirstName,
LastName: member.LastName,
},
}
buf, err := json.Marshal(evReq)
if err != nil {
return err
}
bufIndent, err := json.MarshalIndent(evReq, "", " ")
if err != nil {
return err
}
fmt.Println(string(bufIndent))
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/references/events", baseURL), bytes.NewBuffer(buf))
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Token %s", influitiveToken))
req.Header.Set("X_ORG_ID", influitiveOrgID)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("unable to retrieve details of logged in user: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if body, err := ioutil.ReadAll(resp.Body); err == nil {
fmt.Println(string(body))
}
return fmt.Errorf("influitive did not return good status: %s", resp.Status)
}
return nil
}
func main() {
influitiveToken = os.Getenv("INFLUITIVE_API_TOKEN")
influitiveOrgID = os.Getenv("INFLUITIVE_ORG_ID")
if len(influitiveToken) == 0 || len(influitiveOrgID) == 0 {
panic("Environment variables not set")
}
fmt.Printf("-- Retrieving member with email %s\n", os.Args[1])
testMember, err := getMemberByEmail(os.Args[1])
if err != nil {
panic(err)
}
memberJson, err := json.MarshalIndent(testMember, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(memberJson))
fmt.Println()
fmt.Printf("-- Logging %s event for member\n", os.Args[2])
if err := logEvent(os.Args[2], testMember); err != nil {
panic(err)
}
}
@HokieGeek
Copy link
Author

Usage: go run main.go <email> <event_type>
Example: go run main.go foo@bar.com site_visit

@HokieGeek
Copy link
Author

HokieGeek commented Apr 5, 2022

Output

-- Retrieving member with email foo@bar.com
{
  "id": 7,
  "name": "dummy2",
  "first_name": "dummy2",
  "last_name": "",
  "email": "foo@bar.com",
  "title": "",
  "company": "",
  "uuid": "636c0f75-86ee-4fd6-90a9-d1723aa1ac59",
  "type": "Advocate",
  "created_at": "2022-03-31T00:23:47.164Z",
  "joined_at": "2022-04-05T16:00:06.119Z",
  "locked_at": "",
  "external_ids": {
    "salesforce_account_id": "",
    "salesforce_contact_id": ""
  },
  "match_categories": {
    "SSO Contact ID": "42"
  },
  "custom_fields": {
    "SSO Contact ID": "42"
  },
  "nps_score": 0,
  "current_points": 120640,
  "lifetime_points": 120640,
  "crm_contact_id": "",
  "salesforce_id": "",
  "invite_link": "",
  "language": "en",
  "address": "",
  "level": {
    "id": 1,
    "name": "Insider"
  },
  "source": "apitest",
  "thumb": "https://static.influitive.com/hub-fe/images/contacts/thumb/missing-4992ced3c9.png"
}

-- Logging site_visit event for member
{
 "type": "site_visit",
 "member": {
  "id": 7,
  "email": "foo@bar.com",
  "crm_contact_id": "",
  "first_name": "dummy2",
  "last_name": ""
 },
 "notes": "",
 "link": "",
 "points": 0
}
500 Internal Server Error
If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.
panic: influitive did not return good status: 500 Internal Server Error

goroutine 1 [running]:
main.main()
	/Users/andres/work/src/influitive-log-event-error/main.go:184 +0x318
exit status 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment