def feedback(): if not verify_slack_request(request): return 'Request verification failed' text = request.form.get('text') print('Feedback: ' + str(text)) return 'Thank you for your feedback'
def rateAlert(): if not verify_slack_request(request): return 'Request verification failed' text = request.form.get('text') slack_id = request.form.get('user_id') user = User.query.filter_by(slack_id=slack_id).first() if user is None: return 'Please use /transferwise to connect your TransferWise account' if len(text) != 3: return 'The currency has to be a three letter currency code,\ such as EUR.' if user.home_currency is None: source_currency = 'GBP' else: source_currency = user.home_currency response = getExchangeRate(access_token=user.getToken(), source_currency=source_currency, target_currency=text) if response.status_code != 200: return 'Something went wrong.\ Try to reconnect to TransferWise using /transferwise.' exchange_rate = json.loads(response.text)[0] response_message = 'Rate for ' +\ exchange_rate['source'] +\ "/" + exchange_rate['target'] +\ " is " + str(exchange_rate['rate']) return response_message
def transferwiseCommands(): if not verify_slack_request(request): return 'Request verification failed' commands = available_commands() text = 'Available commands: \n' for c in commands: text += c['command'] + "\n" + c['description'] + "\n \n" return text
def borderless(): if not verify_slack_request(request): return 'Request verification failed' global encryption_key slack_id = request.form.get('user_id') user = User.query.filter_by(slack_id=slack_id).first() if user is None: return 'Please connect your TransferWise account using /transferwise.' else: token = user.getToken() if token is None: return 'Please connect your TransferWise account using /transferwise.' borderless = getBorderlessAccountId(profileId=user.transferwise_profile_id, access_token=token) if borderless.status_code != 200: return 'Please connect your TransferWise account using /transferwise.' if len(json.loads(borderless.text)) < 1: return 'You need to have a borderless account to use this command.' borderlessId = json.loads(borderless.text)[0]['id'] accounts = getBorderlessAccounts(borderlessId=borderlessId, access_token=token) if accounts.status_code != 200: return str(accounts.status_code) accounts = json.loads(accounts.text) text = "Your balances are: \n" for b in accounts['balances']: currency = str(b['amount']['currency']) currency = currency_to_flag(currency) text += currency + " " + str(b['amount']['value']) + \ " " + str(b['amount']['currency']) + "\n" return text
def lastest(): if not verify_slack_request(request): return 'Request verification failed' response_url = request.form.get('response_url') print(response_url) slack_id = request.form.get('user_id') user = User.query.filter_by(slack_id=slack_id).first() if user is None: return 'Please use /transferwise to connect your TransferWise account' profileId = user.transferwise_profile_id if profileId is None or user is None: return 'Please use /transferwise to connect your TransferWise account' else: token = user.getToken() if token is None: return 'Please use /transferwise to \ connect your TransferWise account' activity = get_latest_borderless_activity(profileId, token) return activity
def home_currency(): if not verify_slack_request(request): return 'Request verification failed' home_currency = request.form.get('text') print('Home currency switched to ' + home_currency) if home_currency == "": slack_id = request.form.get('user_id') user = User.query.filter_by(slack_id=slack_id).first() return user.home_currency home_currency = home_currency.upper() supported_currencies = [ 'USD', 'AUD', 'BGN', 'BRL', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'GBP', 'HKD', 'HRK', 'HUF', 'JPY', 'NOK', 'NZD', 'PLN', 'RON', 'SEK', 'SGD', 'TRY' ] if home_currency not in supported_currencies: return "Currency not supported" if is_prod == 'True': slack_id = request.form.get('user_id') else: slack_id = 'UBH7TETRB' user = User.query.filter_by(slack_id=slack_id).first() if user is None: return 'Please connect your Slack account first using \ /transferwise' user.home_currency = home_currency db.session.commit() return "Home currency updated"
def profile(): if not verify_slack_request(request): return 'Request verification failed' slack_id = request.form.get('user_id') user = User.query.filter_by(slack_id=slack_id).first() token = user.getToken() print('Previous TransferWise profile: ' + str(user.transferwise_profile_id)) profiles = getTransferWiseProfiles(access_token=token) if profiles.status_code != 200: print(str(profiles.status_code)) return 'That didn\'t work.\ Please make sure you have connected your account using /transferwise.' if (json.loads(profiles.text) == "[]"): return 'Go to www.transferwise.com to complete your profile.' if (len(json.loads(profiles.text)) == 1): return 'You only have one profile.' personalProfileId = json.loads(profiles.text)[0]['id'] if user.transferwise_profile_id == personalProfileId: user.transferwise_profile_id = json.loads(profiles.text)[1]['id'] db.session.commit() return 'Active TransferWise profile: ' \ + json.loads(profiles.text)[1]['details']['name'] else: user.transferwise_profile_id = json.loads(profiles.text)[0]['id'] db.session.commit() return 'Active TransferWise profile: ' \ + json.loads(profiles.text)[0]['details']['firstName'] + ' ' \ + json.loads(profiles.text)[0]['details']['lastName']
def transferwise(): if not verify_slack_request(request): return 'Request verification failed' text = request.form.get('text') slack_id = request.form.get('user_id') user = User.query.filter_by(slack_id=slack_id).first() session['slack_id'] = slack_id print('Slack ID ' + str(session['slack_id']) + ' added to session.') if text == 'delete': print('Deleting user ' + str(slack_id)) if user is None: return 'TransferWise integration removed.\ Use /transferwise to reconnect.' else: db.session.delete(user) db.session.commit() return 'TransferWise integration removed.\ Use /transferwise to reconnect.' if user is None: user = User(slack_id=slack_id) db.session.add(user) db.session.commit() if user.encrypted_tw_token is not None: token = user.getToken() profiles = getTransferWiseProfiles(access_token=token) if profiles.status_code == 200: return 'TransferWise account connected' return 'Click here to connect your TransferWise account\ https://slackwise.herokuapp.com/connect?slack_id=' + slack_id
def pay(): start_time = time.time() if not verify_slack_request(request): return 'Request verification failed' text = request.form.get('text') print(text) text = text.split(' ') slack_id = request.form.get('user_id') user = User.query.filter_by(slack_id=slack_id).first() token = user.getToken() if user is None or user.encrypted_tw_token is None: return 'Please connect your TransferWise\ account using /transerwise token' profileId = user.transferwise_profile_id if profileId is None: return 'Please connect your TransferWise\ account using /transerwise token' if len(text) < 3: return 'Please use the format /pay email amount currency' if len(text[0].split('@')) < 2: return 'Please include a valid email' recipient_email = text[0] recipient_email = recipient_email.split('|')[1].replace(">", "") print('Recipient email: ' + str(recipient_email)) amount = text[1] print('Amount: ' + str(amount)) currency = text[2].upper() print('Currency: ' + str(currency)) name = recipient_email.split('@')[0].split('.') first_name = name[0] first_name = ''.join([i for i in first_name if not i.isdigit()]) if len(name) < 2: last_name = 'Unknown' else: last_name = name[len(name) - 1] last_name = ''.join([i for i in last_name if not i.isdigit()]) if len(last_name) < 3: last_name = 'Unknown' name = first_name + ' ' + last_name print('Name: ' + name) recipient = createTransferWiseRecipient(email=recipient_email, currency=currency, name=name, legalType='PRIVATE', profileId=profileId, access_token=token) print(recipient) if recipient.status_code == 401: return 'Connect your TransferWise account using /transferwise' if recipient.status_code != 200: return 'Connect your TransferWise account using /transferwise' end_time = time.time() print("Recipient Time: " + str(end_time - start_time)) if user.home_currency is None: sourceCurrency = 'GBP' else: sourceCurrency = user.home_currency quote = createTransferWiseQuote(profileId=profileId, sourceCurrency=sourceCurrency, targetCurrency=currency, access_token=token, targetAmount=amount) print(quote.status_code) if quote.status_code == 422: print("Unsupported currency, defaulting to GBP") sourceCurrency = 'GBP' quote = createTransferWiseQuote(profileId=profileId, sourceCurrency=sourceCurrency, targetCurrency=currency, access_token=token, targetAmount=amount) if quote.status_code == 401: return str(quote.error_message) quoteId = json.loads(quote.text)['id'] print("Quote ID: " + str(quoteId)) end_time = time.time() print("Quote Time: " + str(end_time - start_time)) recipientId = json.loads(recipient.text)['id'] transfer = createPayment(recipientId=recipientId, quoteId=quoteId, reference='Slackwise', access_token=token) if transfer.status_code == 401: return str(json.loads(transfer.text)) end_time = time.time() print("Transfer Time: " + str(end_time - start_time)) transferId = json.loads(transfer.text)['id'] print("Transfer ID: " + str(transferId)) end_time = time.time() print("Total Time: " + str(end_time - start_time)) if transferId == 'Failed to create transfer': return "Failed to pay" else: return 'Click here to pay:\ https://transferwise.com/transferFlow#/transfer/' + str(transferId)