Lambda function "Object of type function is not JSON serializable"

Viewed 25

this is hopefully my last question for this project since everything else is working fine. I can't figure out what's causing this error though. Here is the code for my tweepy bot:

import tweepy
import random
import os
import json
import boto3

read = tweepy.Client(os.getenv('btok'))
write = tweepy.Client(
    consumer_key=os.getenv('ckey'), 
    consumer_secret=os.getenv('csec'), 
    access_token=os.getenv('atok'), 
    access_token_secret=os.getenv('atos')
)

# declaring variables
user_id = 1568306756798775297
greet = random.choice(list(open('greetings.txt'))) # picking a random greeting from the list
reply = random.choice(list(open('replies.txt'))) # picking a random reply from the list
strings = {'hi', 'hello', 'hey', 'hai'} # recognized greetings
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('lastid')

def main(event, context):
    # TODO implement
    reply()
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
def reply():
    
    value = table.get_item(Key={'lastidkey': '1'})
    last = value['Item']['lidval']
    response = read.get_users_mentions(user_id, since_id=last) # fetching new mentions
    for tweet in response.data:
        text = tweet.text
        id = tweet.id
        if any(x in text.lower() for x in strings):
            write.create_tweet(in_reply_to_tweet_id=id, text=greet) # if they're greeting the bot, the bot will greet them back
        else:
            write.create_tweet(in_reply_to_tweet_id=id, text=reply) # otherwise, it's a random reply
        table.put_item(Item={'lastidkey': '1', 'lidval': str(tweet.id)}) # writes latest id to dynamodb   

The "greeting" part works fine, but the part under else: does not work at all. Here is the error message I get:

{
  "errorMessage": "Object of type function is not JSON serializable",
  "errorType": "TypeError",
  "requestId": "cb6423eb-e27e-4229-b322-c6df9fad3b5c",
  "stackTrace": [
    "  File \"/var/task/reply.py\", line 25, in main\n    reply()\n",
    "  File \"/var/task/reply.py\", line 41, in reply\n    write.create_tweet(in_reply_to_tweet_id=id, text=reply) # otherwise, it's a random reply\n",
    "  File \"/opt/python/tweepy/client.py\", line 824, in create_tweet\n    return self._make_request(\n",
    "  File \"/opt/python/tweepy/client.py\", line 126, in _make_request\n    response = self.request(method, route, params=request_params,\n",
    "  File \"/opt/python/tweepy/client.py\", line 83, in request\n    with self.session.request(\n",
    "  File \"/opt/python/requests/sessions.py\", line 573, in request\n    prep = self.prepare_request(req)\n",
    "  File \"/opt/python/requests/sessions.py\", line 484, in prepare_request\n    p.prepare(\n",
    "  File \"/opt/python/requests/models.py\", line 371, in prepare\n    self.prepare_body(data, files, json)\n",
    "  File \"/opt/python/requests/models.py\", line 511, in prepare_body\n    body = complexjson.dumps(json, allow_nan=False)\n",
    "  File \"/var/runtime/simplejson/__init__.py\", line 398, in dumps\n    return cls(\n",
    "  File \"/var/runtime/simplejson/encoder.py\", line 296, in encode\n    chunks = self.iterencode(o, _one_shot=True)\n",
    "  File \"/var/runtime/simplejson/encoder.py\", line 378, in iterencode\n    return _iterencode(o, 0)\n",
    "  File \"/var/runtime/simplejson/encoder.py\", line 272, in default\n    raise TypeError('Object of type %s is not JSON serializable' %\n"
  ]
}

Thanks everyone.

1 Answers

Solved it! In hindsight it was fairly obvious - the variable reply was throwing it off since it shares a name with the function. I renamed it and it works fine now.

Related