python - Telegram bot webhook 和 amazon lambda

标签 python amazon-web-services aws-lambda bots telegram-webhook

我已经尝试创建我的第一个电报机器人,将代码托管为亚马逊 lambda 实例,我想我应该向 webhook 返回一些东西,因为它一直说“来自 webhook 的错误响应:502 Bad Gateway”。

这是我的部分代码:

def msgSend(text, chat_id):
    url = URL + "sendMessage?text={}&chat_id={}".format(text, chat_id)
    response = requests.get(url)
    content = response.content.decode("utf8")
    return content


def handle(msg):
    sender = msg['from']['username']
    id_gruppo = msg['chat']['id']
    if sender == NAME:
        testo = msg['text']
        usernames = [x.replace('@','') for x in rx.findall(text)]
        map(foo, usernames)
        msgSend(confirm_mess, id_group)
        return


def main(event, context): 
    response = ast.literal_eval(event['body'])
    handle(response['message'])
    return {
        'something': 'something'
    }

实际上这个过程运行良好,我的 lambda 接收消息,一切都像魅力一样,除了一件事,确认消息无休止地一遍又一遍地发送,webhooks 从不将消息标记为已读。

这里是 getWebHookInfo 的响应: {“ok”:true,“result”:{“url”:“https://t2rt9guj3h.execute-api.us-west-2.amazonaws.com/prod/instabot”,“has_custom_certificate”:false,“pending_update_count ":19,"last_error_date":1489331750,"last_error_message":"来自 webhook 的错误响应:502 Bad Gateway","max_connections":40}}

根据机器人助手的说法,wh 需要 2XX 代码响应...

有什么想法吗?

最佳答案

According to the bot helper the wh requires a 2XX code response...

这是真的。你的最后陈述应该是

return {
    statusCode: 200
}

如果您没有返回成功的响应代码,Telegram 将不知道如何处理它,这就是您看到 HTTP 502 Bad Gateway 的原因。我也打了一段时间:)

关于python - Telegram bot webhook 和 amazon lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42769881/

相关文章:

python - 两台服务器之间进行通信 - Amazon SQS/SNS?

amazon-web-services - 如何使用 AWS CLI 更新弹性 beantalk 代码?

amazon-web-services - Terraform AWS S3 - 拒绝除特定用户以外的所有人

python - Python中是否有用于纯文本文件的 native 模板系统?

amazon-web-services - 在亚马逊 AWS 上部署 TCP/IP 和 UDP 服务的最佳架构(无 EC2 实例)

amazon-web-services - LambdaContext 对象没有属性 getRemainingTimeInMillis

node.js - 如何在 AWS Lambda 中使用 Node pg?

python - Reddit 搜索 API 未提供所有结果

连接到使用 Nodejs 创建的 socket.io 服务器的 Python 脚本

python - 将 x 和 y 抖动添加到 seaborn 线性图中是否会改变拟合值?