def get_spotify_oauth(bot=False): client_id = get_setting(SPOTIFY_CLIENT_ID) client_secret = get_setting(SPOTIFY_CLIENT_SECRET) redirect_uri = get_setting(SPOTIFY_REDIRECT_URI) scopes = OAUTH_SCOPES if bot: redirect_uri = get_setting(ADD_BOT_REDIRECT_URI) scopes = BOT_SCOPES spotify_oauth = oauth2.SpotifyOAuth( client_id, client_secret, redirect_uri, scope=scopes) return spotify_oauth
def _send_email(to, subject, text, html, bcc_list=None, additional_data=None): if not is_deployed(): app.logger.info(text) return try: ses = boto3.client('ses', region_name='us-east-1') response = ses.send_email( Destination={ 'ToAddresses': [to], 'BccAddresses': bcc_list if bcc_list else [], }, Message={ 'Body': { 'Html': { 'Charset': 'UTF-8', 'Data': html, }, 'Text': { 'Charset': 'UTF-8', 'Data': text, } }, 'Subject': { 'Charset': 'UTF-8', 'Data': subject, } }, Source=get_setting(NOTIFICATION_SENDER), ) except Exception as e: app.logger.exception(u'Mail send failed w/ exception: %s', str(e)) return
def verify(): # When the endpoint is registered as a webhook, it must echo back # the 'hub.challenge' value it receives in the query arguments hub_mode = request.args.get('hub.mode') hub_challenge = request.args.get('hub.challenge') if hub_mode == 'subscribe' and hub_challenge: # Verify request is from Facebook hub_token = request.args.get('hub.verify_token') verify_token = get_setting(MESSENGER_VERIFY_TOKEN) if hub_token != verify_token: return 'TOKEN MISMATCH', httplib.UNAUTHORIZED return hub_challenge, httplib.OK return 'Hello World', httplib.OK
def send_message(recipient_id, message_text): app.logger.warn("Sending message: %s: %s", recipient_id, message_text) access_token = get_setting(MESSENGER_PAGE_ACCESS_TOKEN) params = {"access_token": access_token} headers = {"Content-Type": "application/json"} data = json.dumps({ "recipient": {"id": recipient_id}, "message": {"text": message_text} }) r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) if r.status_code == httplib.OK: app.logger.warn("Message succeeded") else: app.logger.warn("Message failed. Status: %s", r.status_code)
def get_botify(bot_id=None): if bot_id is None: bot_id = get_setting(SPOTIFY_BOT_USERNAME) bot = select_bot(bot_id) if not bot: return None, None # If the token has expired or will expire very soon, refresh it if bot.expires_at < (int(time()) + 10): app.logger.debug('Bot %s access expired. Refreshing.', bot_id) oauth = get_spotify_oauth() token_info = oauth.refresh_access_token(bot.refresh_token) access_token = token_info['access_token'] refresh_token = token_info['refresh_token'] expires_at = token_info['expires_at'] bot = update_bot(bot_id, access_token, refresh_token, expires_at) return bot_id, Spotify(bot.access_token)