|
| 1 | +import requests |
| 2 | +from properties import * |
| 3 | +from pprint import pprint |
| 4 | +import datetime |
| 5 | + |
| 6 | + |
| 7 | +def is_authenticated(zv_user, tg_id): |
| 8 | + # TODO |
| 9 | + pass |
| 10 | + |
| 11 | + |
| 12 | +# optional params, will not be used in testing |
| 13 | +def connect(zv_user, tg_id, first_name="He/She", bot=None, msg=None): |
| 14 | + # check if zv_user already exists; duplicate users results in 503 error from VIVID API |
| 15 | + should_add_connection_to_hepir_db = False |
| 16 | + vivid_request = requests.get('{}/api/v1/user-registrations/{}'.format(VIVID_ROOT_URL, zv_user), |
| 17 | + auth=(VIVID_USER, VIVID_PASSWORD)) |
| 18 | + |
| 19 | + print( |
| 20 | + '[{}] The status code of the vivid_request ([{}]: {}) is: {}'.format(str(datetime.datetime.now()).split('.')[0], |
| 21 | + vivid_request.request, |
| 22 | + vivid_request.url, |
| 23 | + vivid_request.status_code)) |
| 24 | + |
| 25 | + # status_code == 200 |
| 26 | + # USE CASE 1: zv_user registered in Zevere and connected with Calista but NOT HepiR |
| 27 | + if vivid_request.status_code == 200: |
| 28 | + print('[{}] User registration found for ({})'.format( |
| 29 | + str(datetime.datetime.now()).split('.')[0], zv_user)) |
| 30 | + pprint(vivid_request.json()) |
| 31 | + should_add_connection_to_hepir_db = True |
| 32 | + |
| 33 | + # status_code == 400 |
| 34 | + # USE CASE 2: zv_user is not registered in Zevere |
| 35 | + elif vivid_request.status_code == 400: |
| 36 | + print('[{}] User ID ({}) is invalid or does not exist'.format(str(datetime.datetime.now()).split('.')[0], |
| 37 | + zv_user)) |
| 38 | + pprint(vivid_request.json()) |
| 39 | + # should never get here because can only access Login Widget workflow on Profile page after logging in via Coherent |
| 40 | + |
| 41 | + # status_code == 404 |
| 42 | + # USE CASE 3: zv_user is registered but not connected to Calista or HepiR |
| 43 | + elif vivid_request.status_code == 404: |
| 44 | + print('[{}] User ({}) has not registered with any of the Zevere chat bots'.format( |
| 45 | + str(datetime.datetime.now()).split('.')[0], zv_user)) |
| 46 | + pprint(vivid_request.json()) |
| 47 | + should_add_connection_to_hepir_db = True |
| 48 | + |
| 49 | + # connect zv_user to tg_id if not exists in hepir db |
| 50 | + # send post with { |
| 51 | + # "username": "string", |
| 52 | + # "chatUserId": "string" |
| 53 | + # } |
| 54 | + # as payload to vivid api |
| 55 | + if should_add_connection_to_hepir_db: |
| 56 | + # if user connection already in hepir db, don't add again |
| 57 | + if user_collection.find_one({ |
| 58 | + 'zv_user': zv_user, |
| 59 | + 'tg_id': str(tg_id) |
| 60 | + }): |
| 61 | + print('[{}] (zv_user={}, tg_id={}) found in (db={}) => {} is a returning user :)!'.format( |
| 62 | + str(datetime.datetime.now()).split('.')[0], zv_user, tg_id, MONGODB_DBNAME, first_name)) |
| 63 | + print('[{}] will not be adding (zv_user={}, tg_id={}) to (db={})'.format( |
| 64 | + str(datetime.datetime.now()).split('.')[0], zv_user, |
| 65 | + tg_id, MONGODB_DBNAME)) |
| 66 | + |
| 67 | + requests.get( |
| 68 | + 'https://api.telegram.org/bot{}/sendMessage?chat_id={}&text=You are already logged into Zevere with the id of `{}`!&parse_mode=Markdown'.format( |
| 69 | + TOKEN, tg_id, zv_user)) |
| 70 | + |
| 71 | + return True, 'You are already logged into Zevere with the id of {}.'.format(zv_user) |
| 72 | + |
| 73 | + # if zv_user connected to another tg_id, don't connect |
| 74 | + elif user_collection.find_one({ |
| 75 | + 'zv_user': zv_user |
| 76 | + }): |
| 77 | + requests.get( |
| 78 | + 'https://api.telegram.org/bot{}/sendMessage?chat_id={}&text=Zevere User ID `{}` is already connected to another telegram account!&parse_mode=Markdown'.format( |
| 79 | + TOKEN, tg_id, zv_user)) |
| 80 | + |
| 81 | + return False, "{} is already connected to another telegram account!".format(zv_user) |
| 82 | + |
| 83 | + # if tg_id connected to another zv_user, don't connect |
| 84 | + elif user_collection.find_one({ |
| 85 | + 'tg_id': str(tg_id) |
| 86 | + }): |
| 87 | + requests.get( |
| 88 | + 'https://api.telegram.org/bot{}/sendMessage?chat_id={}&text=Your telegram account is already connected to another Zevere ID!&parse_mode=Markdown'.format( |
| 89 | + TOKEN, tg_id)) |
| 90 | + |
| 91 | + return False, "Your telegram account is already connected to another Zevere ID!" |
| 92 | + |
| 93 | + # zv user - tg user connection not found in hepir db => add connection to hepir db |
| 94 | + else: |
| 95 | + print( |
| 96 | + '[{}] No users found matching (zv_user={}, tg_id={}) in (db={})'.format( |
| 97 | + str(datetime.datetime.now()).split('.')[0], |
| 98 | + zv_user, tg_id, MONGODB_DBNAME)) |
| 99 | + print('[{}] adding (zv_user={}, tg_id={}) to (db={})...'.format(str(datetime.datetime.now()).split('.')[0], |
| 100 | + zv_user, tg_id, MONGODB_DBNAME)) |
| 101 | + new_user_id = user_collection.insert_one({ |
| 102 | + 'zv_user': zv_user, |
| 103 | + 'tg_id': tg_id |
| 104 | + }).inserted_id |
| 105 | + print('[{}] new user (zv_user={}, tg_id={}) inserted into (db={}): (inserted_id={})'.format( |
| 106 | + str(datetime.datetime.now()).split('.')[ |
| 107 | + 0], zv_user, tg_id, MONGODB_DBNAME, |
| 108 | + new_user_id)) |
| 109 | + |
| 110 | + # send post request to vivid api with payload containing zv user and tg user id |
| 111 | + vivid_request = requests.post('{}/api/v1/user-registrations'.format(VIVID_ROOT_URL), |
| 112 | + json={"username": zv_user, |
| 113 | + "chatUserId": tg_id}, |
| 114 | + auth=(VIVID_USER, VIVID_PASSWORD)) |
| 115 | + |
| 116 | + print('vivid_request is: {}'.format(vivid_request)) |
| 117 | + print('[{}] The status code of the vivid_request ([{}]: {}) is: {}'.format( |
| 118 | + str(datetime.datetime.now()).split('.')[0], |
| 119 | + vivid_request.request, |
| 120 | + vivid_request.url, |
| 121 | + vivid_request.status_code)) |
| 122 | + print('[{}] The json of the vivid_request ([{}]: {}) is: {}'.format( |
| 123 | + str(datetime.datetime.now()).split('.')[0], |
| 124 | + vivid_request.request, |
| 125 | + vivid_request.url, |
| 126 | + vivid_request.json())) |
| 127 | + |
| 128 | + if vivid_request.status_code == 201: |
| 129 | + print( |
| 130 | + '[{}] Zevere User (zv_user={}) has been succesfully connected to telegram account (tg_id={})'.format( |
| 131 | + str(datetime.datetime.now()).split('.')[0], zv_user, tg_id)) |
| 132 | + requests.get( |
| 133 | + 'https://api.telegram.org/bot{}/sendMessage?chat_id={}&text=You have successfully connected your telegram account to the Zevere ID: `{}`!&parse_mode=Markdown'.format( |
| 134 | + TOKEN, tg_id, zv_user)) |
| 135 | + |
| 136 | + return True, 'You have successfully logged into the Zevere account {} with your telegram account of {}'.format(zv_user, tg_id) |
| 137 | + |
| 138 | + elif vivid_request.status_code == 400: |
| 139 | + print( |
| 140 | + '[{}] There are invalid fields in the POST request to VIVID API or the username (zv_user={}) does not exist on Zevere'.format( |
| 141 | + str(datetime.datetime.now()).split('.')[0], zv_user)) |
| 142 | + |
| 143 | + # remove the entry we added to the hepir db, rollback transaction. |
| 144 | + user_collection.delete_one({ |
| 145 | + 'zv_user': zv_user, |
| 146 | + 'tg_id': str(tg_id) |
| 147 | + }) |
| 148 | + |
| 149 | + requests.get( |
| 150 | + 'https://api.telegram.org/bot{}/sendMessage?chat_id={}&text=Unable to connect your telegram account to the Zevere ID: `{}` due to internal server errors!&parse_mode=Markdown'.format( |
| 151 | + TOKEN, tg_id, zv_user)) |
| 152 | + |
| 153 | + return False, 'You have provided invalid login credentials.' |
| 154 | + |
| 155 | + else: |
| 156 | + # sends feedback to user confirming login |
| 157 | + requests.get( |
| 158 | + 'https://api.telegram.org/bot{}/sendMessage?chat_id={}&text=Zevere User ID `{}` is invalid or does not exist!&parse_mode=Markdown'.format( |
| 159 | + TOKEN, tg_id, zv_user)) |
| 160 | + |
| 161 | + return False, 'You have provided invalid login credentials.' |
| 162 | + |
| 163 | + return False, 'You have provided invalid login credentials.' |
| 164 | + |
| 165 | + |
| 166 | +# optional params here, will not be used in testing |
| 167 | +def disconnect(zv_user, tg_id, bot=None, msg=None): |
| 168 | + # send DELETE request to vivid to remove the associations of an existing Zevere user to the Zevere chat bots. |
| 169 | + should_remove_connection_from_hepir_db = False |
| 170 | + vivid_request = requests.delete('{}/api/v1/user-registrations/{}'.format(VIVID_ROOT_URL, zv_user), |
| 171 | + auth=(VIVID_USER, VIVID_PASSWORD)) |
| 172 | + |
| 173 | + print( |
| 174 | + '[{}] The status code of the vivid_request ([{}]: {}) is: {}'.format(str(datetime.datetime.now()).split('.')[0], |
| 175 | + vivid_request.request, |
| 176 | + vivid_request.url, |
| 177 | + vivid_request.status_code)) |
| 178 | + # Reference from documentation provided at: https://zv-botops-vivid.herokuapp.com/api/docs/swagger/index.html |
| 179 | + |
| 180 | + # status_code == 204 |
| 181 | + # Registration is deleted |
| 182 | + if vivid_request.status_code == 204: |
| 183 | + print('[{}] Registration is deleted for ({})'.format( |
| 184 | + str(datetime.datetime.now()).split('.')[0], zv_user)) |
| 185 | + should_remove_connection_from_hepir_db = True |
| 186 | + |
| 187 | + # status_code == 400 |
| 188 | + # User ID is invalid or does not exist |
| 189 | + elif vivid_request.status_code == 400: |
| 190 | + print('[{}] User ID ({}) is invalid or does not exist'.format(str(datetime.datetime.now()).split('.')[0], |
| 191 | + zv_user)) |
| 192 | + pprint(vivid_request.json()) |
| 193 | + |
| 194 | + # status_code == 404 |
| 195 | + # User has not registered with any of the Zevere chat bots |
| 196 | + elif vivid_request.status_code == 404: |
| 197 | + print('[{}] User ({}) has not registered with any of the Zevere chat bots'.format( |
| 198 | + str(datetime.datetime.now()).split('.')[0], zv_user)) |
| 199 | + pprint(vivid_request.json()) |
| 200 | + |
| 201 | + if should_remove_connection_from_hepir_db: |
| 202 | + # remove user connection from hepir db, if it exists |
| 203 | + if user_collection.find_one({ |
| 204 | + 'zv_user': zv_user, |
| 205 | + 'tg_id': str(tg_id) |
| 206 | + }): |
| 207 | + result = user_collection.delete_one({ |
| 208 | + 'zv_user': zv_user, |
| 209 | + 'tg_id': str(tg_id) |
| 210 | + }).deleted_count |
| 211 | + |
| 212 | + if result == 1: |
| 213 | + print('[{}] Successfully removed {} user with zv_user={} and tg_id={} from the HepiR database.'.format( |
| 214 | + str(datetime.datetime.now()).split('.')[0], result, zv_user, tg_id)) |
| 215 | + |
| 216 | + if bot is not None: |
| 217 | + bot.send_message(msg.chat.id, 'You have successfully disconnected your telegram account from the Zevere ID: `{}`'.format(zv_user), |
| 218 | + parse_mode="Markdown" |
| 219 | + ) |
| 220 | + return True, 'You have successfully been logged out from {}'.format(zv_user) |
| 221 | + |
| 222 | + else: |
| 223 | + print('[{}] Failed to remove user with zv_user={} and tg_id={} from the HepiR database.'.format( |
| 224 | + str(datetime.datetime.now()).split('.')[0], zv_user, tg_id)) |
| 225 | + return False, 'You have failed to log out from {}'.format(zv_user) |
| 226 | + |
| 227 | + # if user connection does not exist in hepir db, don't need to remove |
| 228 | + else: |
| 229 | + if bot is not None: |
| 230 | + bot.send_message(msg.chat.id, 'Your telegram account is not connected to the Zevere ID: `{}`'.format(zv_user), |
| 231 | + parse_mode="Markdown" |
| 232 | + ) |
| 233 | + return False, 'You are not logged in to any Zevere account.' |
| 234 | + |
| 235 | + return False, 'You are not logged in to any Zevere account.' |
0 commit comments