Created
February 3, 2017 18:51
-
-
Save alairock/dee5a6a040516a11d07c44ab5bf40a03 to your computer and use it in GitHub Desktop.
Mass update intercom user_id's and/or emails.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from intercom import Intercom, User, errors | |
import requests | |
_APP_ID = "arst234" | |
_APP_KEY = "arsttsrdhwfpgj3245rdsrsp2453rstd" | |
_DBNAME = "arst" | |
_DBUSER = "arst" | |
_DBHOST = "localhost" | |
_DBPASS = "arst" | |
Intercom.app_id = _APP_ID | |
Intercom.app_api_key = _APP_KEY | |
try: | |
import psycopg2 | |
import psycopg2.extras | |
conn = None | |
try: | |
conn = psycopg2.connect("dbname='{dbname}' user='{dbuser}' host='{dbhost}' password='{dbpass}'" | |
.format(dbname=_DBNAME, dbuser=_DBUSER, dbhost=_DBHOST, dbpass=_DBPASS)) | |
except Exception: | |
print("I am unable to connect to the database") | |
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) | |
cur.execute('SELECT * FROM users') | |
rows = cur.fetchall() | |
fails = [] | |
for row in rows: | |
print('({id}, {email}, {new_user_id})'.format(id=row['id'], email=row['email'], new_user_id=row['new_user_id'])) | |
# In the event you have existing records with the new user_id, let's assume you don't want them. | |
# WARNING: You can A) Only do this once and B) Should only do it if you are trying to reconcile | |
# and dedupe new/existing records in intercom | |
try: | |
string_user = User.find(email=row['email'], user_id=row['new_user_id']) | |
string_user.delete() | |
except errors.ResourceNotFound: | |
print('Does not exist, skipping delete') | |
except: | |
print('Something bad must have happend.') | |
try: | |
u = User.find(email=row['email'], user_id=row['id']) | |
print(u.id) | |
headers = {'Accept': 'application/json'} | |
resp = requests.request( | |
method='POST', url='https://api.intercom.io/users', | |
headers=headers, | |
json={ | |
"id": u.id, | |
"user_id": row['new_user_id'], | |
'email': row['email'], | |
}, | |
auth=(_APP_ID, _APP_KEY)) | |
except errors.ResourceNotFound: | |
fails.append(row['email']) | |
print('User Does Not Exist with Int ID:', row) | |
print('Fails:', fails) | |
except KeyboardInterrupt as e: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment