コード例 #1
0
class crm():
    ################################
    # data and methods for the crm #
    ################################

    def __init__(self, insight_key):
        self.crm = Insightly(apikey=insight_key)
        self.load_insightly_contacts()
        self.load_insightly_orgs()

    def load_insightly_contacts(self):
        ###################################
        # load contact ids from insightly #
        ###################################
        self.ins_contacts = {}
        conns = self.crm.getContacts()
        for c in conns:
            self.ins_contacts["%s %s" % (c['FIRST_NAME'],
                                         c['LAST_NAME'])] = c['CONTACT_ID']

    def addContact(self, dets, who):
        ###################################
        # add a new contact, also a new   #
        # org if required                 #
        ###################################
        c = {}
        c['CONTACT_ID'] = 0  # id=0 means add a new one
        c['FIRST_NAME'] = dets["first-name"]
        c['LAST_NAME'] = dets["last-name"]
        c['BACKGROUND'] = dets["summary"]
        # Tags for location and who owns the contact in Linked-In
        c['TAGS'] = []
        c['TAGS'].append(
            {"TAG_NAME": "Location-%s" % dets["location-country"].upper()})
        c['TAGS'].append({"TAG_NAME": "LIContact-%s" % who})

        c['IMAGE_URL'] = dets['pictureUrl']

        linkedinurl = "https://www.linkedin.com/profile/view?%s" % dets['id']

        c["CONTACTINFOS"] = [{
            "SUBTYPE": "LinkedInPublicProfileUrl",
            "TYPE": "SOCIAL",
            "DETAIL": linkedinurl,
            "LABEL": "LinkedInPublicProfileUrl"
        }]

        # find org
        c['LINKS'] = []
        l = {}
        if self.orgs.has_key(dets['company']):
            c['DEFAULT_LINKED_ORGANISATION'] = self.orgs[dets['company']]
            l['ORGANISATION_ID'] = self.orgs[dets['company']]
        else:
            c['DEFAULT_LINKED_ORGANISATION'] = self.addOrg(dets['company'])
            l['ORGANISATION_ID'] = self.addOrg(dets['company'])
        l['ROLE'] = dets['title']
        c['LINKS'].append(l)

        print json.dumps(c, indent=3)

        self.crm.addContact(c)

    def addOrg(self, name):
        ###################################
        # add a new organisation          #
        ###################################
        c = {}
        c['ORGANISATION_NAME'] = name
        resp = self.crm.addOrganization(c)
        return (resp['ORGANISATION_ID'])


###################################
# load org ids from insightly     #
###################################

    def load_insightly_orgs(self):
        o = self.crm.getOrganizations()
        self.orgs = {}
        for x in o:
            self.orgs[x['ORGANISATION_NAME']] = x['ORGANISATION_ID']
コード例 #2
0
class crm():
    ################################
    # data and methods for the crm #
    ################################

    def __init__(self, insight_key):

        print "Connecting to Insightly..."

        # link to the crm
        self.crm = Insightly(apikey=insight_key)

        # load all contacts and orgs from crm
        self.load_insightly_contacts()
        self.load_insightly_orgs()

    def load_insightly_contacts(self):

        # load contact ids from insightly #
        self.ins_contacts = {}
        conns = self.crm.getContacts()
        for c in conns:
            self.ins_contacts["%s %s" % (c['FIRST_NAME'],
                                         c['LAST_NAME'])] = c['CONTACT_ID']

    def addContact(self, dets, who):

        # add a new contact, also a new org if required
        c = {}
        c['CONTACT_ID'] = 0  # id=0 means add a new one
        c['FIRST_NAME'] = dets["first-name"]
        c['LAST_NAME'] = dets["last-name"]
        c['BACKGROUND'] = dets["summary"]

        # Tags for location and who owns the contact in Linked-In
        c['TAGS'] = []
        if dets["location-country"] <> None:
            c['TAGS'].append(
                {"TAG_NAME": "Location-%s" % dets["location-country"].upper()})
        c['TAGS'].append({"TAG_NAME": "LIContact-%s" % who})

        # linkedIn URL

        c["CONTACTINFOS"] = [{
            "SUBTYPE": "LinkedInPublicProfileUrl",
            "TYPE": "SOCIAL",
            "DETAIL": dets['linkedInUrl'],
            "LABEL": "linkedInPublicProfileUrl"
        }]

        # Add email address if we have one
        if dets['email'] <> None:
            c["CONTACTINFOS"].append({
                "TYPE": "EMAIL",
                "DETAIL": dets['email'],
                "LABEL": "Work"
            })

# See if we can find a matching organisation
        c['LINKS'] = []
        l = {}
        if self.orgs.has_key(dets['company']):
            c['DEFAULT_LINKED_ORGANISATION'] = self.orgs[dets['company']]
            l['ORGANISATION_ID'] = self.orgs[dets['company']]

        else:
            # no match, so add one
            l['ORGANISATION_ID'] = self.addOrg(dets['company'])

# add job title
        l['ROLE'] = dets['title']
        c['LINKS'].append(l)

        # add contact record to crm
        try:
            c = self.crm.addContact(c)
        except urllib2.HTTPError as e:
            print "Error adding contact."
            print e
            print json.dumps(c)
            sys.exit()

# add to in memory list
        self.ins_contacts["%s %s" %
                          (c['FIRST_NAME'], c['LAST_NAME'])] = c['CONTACT_ID']

        # Update image
        if dets['pictureUrl'] <> None:
            img = urllib2.urlopen(dets['pictureUrl']).read()
            self.addPicture(c['CONTACT_ID'], img)

    def addPicture(self, id, picturestream):
        callREST("PUT", "/v2.1/Contacts/%s/Image/name.jpg" % str(id),
                 picturestream)

    def addPicturetoName(self, name, picturestream):

        # add a picture to a name
        if self.ins_contacts.has_key(name):
            id = self.ins_contacts[name]
            self.addPicture(id, picturestream)
        else:
            print "%s - name not found in Insighly" % name

    def addEmailtoName(self, name, email, label="WORK"):

        # add an email to a name
        if self.ins_contacts.has_key(name):
            id = self.ins_contacts[name]
            self.addEmail(id, email, label)
        else:
            print "%s - name not found in Insighly" % name

    def addEmail(self, id, email, label="WORK"):

        # add email to an id

        # get the record
        c = self.crm.getContact(id)

        # add email
        c["CONTACTINFOS"].append({
            "TYPE": "EMAIL",
            "DETAIL": email,
            "LABEL": label
        })

        # save
        self.crm.addContact(c)

    def addPhonetoName(self, name, phone, label="WORK"):

        # add a phone number to a name
        if self.ins_contacts.has_key(name):
            id = self.ins_contacts[name]
            self.addPhone(id, phone, label)
        else:
            print "%s - name not found in Insighly" % name

    def addPhone(self, id, phone, label="WORK"):

        # add phone number to an id

        # get the record
        c = self.crm.getContact(id)

        # add email
        c["CONTACTINFOS"].append({
            "TYPE": "PHONE",
            "DETAIL": phone,
            "LABEL": label
        })

        # save
        self.crm.addContact(c)

    def addOrg(self, name):

        # add a new organisation
        c = {}
        c['ORGANISATION_NAME'] = name
        resp = self.crm.addOrganization(c)

        # add to list of organisations in memory
        self.orgs[name] = resp['ORGANISATION_ID']

        # return id
        return (resp['ORGANISATION_ID'])

    def load_insightly_orgs(self):

        # load org ids from insightly
        o = self.crm.getOrganizations()
        self.orgs = {}
        for x in o:
            self.orgs[x['ORGANISATION_NAME']] = x['ORGANISATION_ID']

    def checkDetails(self, id, name, who):

        # check for an existing entry if it is in step with linkedIn.
        # Extend later. For not it just appends a tag if missing

        # get contact details for supplied id
        contact = self.crm.getContact(id)

        # create tag to add for owner
        tag = {"TAG_NAME": "LIContact-%s" % who}

        # add tag if needed
        if contact.has_key("TAGS"):
            t = contact["TAGS"]
            if tag not in t:
                t.append(tag)
                print "Adding tag %s to %s %s" % (tag, contact['FIRST_NAME'],
                                                  contact['LAST_NAME'])
                contact["TAGS"] = t
                self.crm.addContact(contact)


# no Tags so add from scratch
        else:
            t = [tag]
            contacts["TAGS"] = t
            self.crm.addContact(contact)