λ函数"Object of type function is not JSON serializable"



这是我对这个项目的最后一个问题,因为其他一切都很好。不过,我不知道是什么原因导致了这个错误。这是我的花呢机器人的代码:

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   

";问候语";部件工作正常,但else:下的部件根本不工作。这是我得到的错误消息:

{
"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 mainn    reply()n",
"  File "/var/task/reply.py", line 41, in replyn    write.create_tweet(in_reply_to_tweet_id=id, text=reply) # otherwise, it's a random replyn",
"  File "/opt/python/tweepy/client.py", line 824, in create_tweetn    return self._make_request(n",
"  File "/opt/python/tweepy/client.py", line 126, in _make_requestn    response = self.request(method, route, params=request_params,n",
"  File "/opt/python/tweepy/client.py", line 83, in requestn    with self.session.request(n",
"  File "/opt/python/requests/sessions.py", line 573, in requestn    prep = self.prepare_request(req)n",
"  File "/opt/python/requests/sessions.py", line 484, in prepare_requestn    p.prepare(n",
"  File "/opt/python/requests/models.py", line 371, in preparen    self.prepare_body(data, files, json)n",
"  File "/opt/python/requests/models.py", line 511, in prepare_bodyn    body = complexjson.dumps(json, allow_nan=False)n",
"  File "/var/runtime/simplejson/__init__.py", line 398, in dumpsn    return cls(n",
"  File "/var/runtime/simplejson/encoder.py", line 296, in encoden    chunks = self.iterencode(o, _one_shot=True)n",
"  File "/var/runtime/simplejson/encoder.py", line 378, in iterencoden    return _iterencode(o, 0)n",
"  File "/var/runtime/simplejson/encoder.py", line 272, in defaultn    raise TypeError('Object of type %s is not JSON serializable' %n"
]
}

谢谢大家。

解决了它!事后看来,这是非常明显的——变量reply将其丢弃,因为它与函数共享一个名称。我重新命名了它,它现在运行良好。

相关内容

  • 没有找到相关文章