在pythonanywhere

时间:2017-12-15 12:17:11

标签: python flask webhooks pythonanywhere python-telegram-bot

我正试图通过webhook运行电报机器人。我的python和flask版本是3.5。 telebot图书馆。

我的代码是:

from flask import Flask
import flask
import telebot

import requests
import time

app = Flask(__name__)

API_TOKEN = '***'

bot = telebot.TeleBot(API_TOKEN)

这是为了获取我的IP:

res=requests.get('http://ipinfo.io/ip')
s=str(res.text)
s=s.split('\n')[0]

WEBHOOK_SSL_CERT = '/home/w/webhook_cert.pem'
WEBHOOK_SSL_PRIV = '/home/w//webhook_pkey.pem'


bot.set_webhook(url="https://myacc.pythonanywhere.com", certificate=open('/home/adr/webhook_pkey.pem'))

WEBHOOK_HOST =s
#WEBHOOK_HOST = 'https://myacc.pythonanywhere.com/'
WEBHOOK_PORT = 443
WEBHOOK_LISTEN = '0.0.0.0'

WEBHOOK_URL_BASE = "https://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)
#updates = bot.get_updates()

处理webhook调用:

@app.route(WEBHOOK_URL_PATH, methods=['POST'])
def webhook():
    if flask.request.headers.get('content-type') == 'application/json':
        json_string = flask.request.get_data().decode('utf-8')
        update = telebot.types.Update.de_json(json_string)
        bot.process_new_updates([update])
        return 'Hello flask'
    else:
        flask.abort(403)

它没有用,我在日志中没有错误。如何通过webhook使电报机器人在pythonanywhere上工作?

0 个答案:

没有答案