Not receiving notification payloads from stream aws lambda python

EDIT: I got it working.

I currently have an AWS Lambda set up with an API Gateway. When prompted with Postman and twitch, it returns the hub.challenge, but I’m not sure if I’m doing that part correctly (new to callback handlers):
#!/usr/bin/env python3
from SendEmail import send_email
from SendText import send_text
import json
import os

def response(message, code):
    return {
        'statusCode': code,
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps(message)

# Callback handler that receives either 1) a challenge for verifying
# a webhook subsciption or 2) a notification payload with details about
# a streamer going on/off-line, in which case we write to message.txt and
# then send a text and email
# *print function is used for cloud watch logging*
def lambda_handler(event, context):
    if event.get('queryStringParameters') and event['queryStringParameters'].get('hub.challenge'): # must not be null
        #print('Hub challenge responded')
        message = event['queryStringParameters']['hub.challenge']
        return response(message, 200)
    elif event.get('body') and event['body'].get('data') != None: # notification payload exists, empty array was being interpreted as None, specify distinction
        body = event['body']
        if len(body.get('data')) > 0: # data is well-formed (would be size 1 with {data here})
            #print('Send email and text')
            with open('/tmp/message.txt', 'w') as msg: # update message file
                start_time = body['data'][0]['started_at']
                stream_title = body['data'][0]['title']
                msg.write("Now streaming since %s: %s" % (start_time, stream_title))
            # data.json must be in same path as current file
            with open('/'.join([os.path.dirname(__file__), 'data.json']), 'r') as f: # send email/text
                data = json.load(f)
            message = 'Successful email and text send!'
            return response(message, 200)
        else: # data is an empty array, stream offline
            #print('Stream Offline.')
            message = 'Stream Offline.'
            return response(message, 200)
        #print('Something went wrong/is malformed.')
        message = 'Something went wrong/is malformed.'
        return response(message, 500)

What I’m trying to do:
I’m writing a Python program that sends me an email and a text when a specific streamer goes live. I have the email and text features running fine. I’m using AWS Lambda and API Gateway feature to receive get/post requests. I don’t have any security measures in place yet since I’m trying to get everything else working.

For reference, here’s the part where I post:

Python 3.7, import request, printing the request object status code is 202, so valid client id and all that, but that’s the same response even with what I think is an incorrect callback url. My current callback url is a link to an API Gateway that’s connected to a lambda function.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.