Webhook上的电报机器人与烧瓶

时间:2018-06-15 14:41:21

标签: python flask bots telegram webhooks

我尝试创建Telegram bot,我正在使用:

  • Ubuntu 18
  • python 3.6
  • pyTelegramBotAPI
  • 烧瓶

这是我的代码:

API_TOKEN = 'My_Token'
WEBHOOK_HOST = 'My_ip'
WEBHOOK_PORT = 8443  # 443, 80, 88 or 8443 (port need to be 'open')
WEBHOOK_LISTEN = '0.0.0.0'  # In some VPS you may need to put here the IP addr

WEBHOOK_SSL_CERT = './webhook_cert.pem'  # Path to the ssl certificate
WEBHOOK_SSL_PRIV = './webhook_pkey.pem'  # Path to the ssl private key

WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)

logger = telebot.logger
telebot.logger.setLevel(logging.INFO)

bot = telebot.TeleBot(API_TOKEN)

apihelper.proxy = {'https': 'socks5://login:password@ip:port'}

app = flask.Flask(__name__)


# Empty webserver index, return nothing, just http 200
@app.route('/', methods=['GET', 'HEAD'])
def index():
    return ''


# Process webhook calls
@app.route(WEBHOOK_URL_PATH, methods=['GET', 'POST'])
def webhook():
    print(flask.request.headers)
    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 ''
    else:
        print('You NOT made it!')
        flask.abort(403)


# Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
    bot.reply_to(message,
                 ("Hi there, I am EchoBot.\n"
                  "I am here to echo your kind words back to you."))


# Handle all other messages
@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
    bot.reply_to(message, message.text)


# Remove webhook, it fails sometimes the set if there is a previous webhook
bot.remove_webhook()

time.sleep(0.1)

# Set webhook
bot.set_webhook(url=WEBHOOK_URL_BASE+WEBHOOK_URL_PATH,
                certificate=open(WEBHOOK_SSL_CERT, 'r'))

# Start flask server
app.run(host=WEBHOOK_LISTEN,
        port=WEBHOOK_PORT,
        ssl_context=(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV),
        debug=True)

我的程序开始很好,但如果我通过bot发送消息没有任何反应。

计划的输出:

* Serving Flask app "bot" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on https://0.0.0.0:8443/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 284-565-338

如果我尝试转到https://0.0.0.0:8443/,python就会发送给我,我知道它没问题:

127.0.0.1 - - [15/Jun/2018 17:12:17] "GET / HTTP/1.1" 200 

但如果我尝试去https://my_ip/my_token/,python就会发送给我:

my_ip - - [15/Jun/2018 17:13:00] "GET /my_token/ HTTP/1.1" 403 -

我已经创建了自签名证书。 也许问题的原因是代理使用?

我从https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/webhook_examples/webhook_flask_echo_bot.py

中抽取了一个例子

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您正在发送GET请求。可以在响应标头GET /my_token/ HTTP/1.1中看到。

尝试向JSON负载和正确设置为application / json的内容类型标头的https://my_ip/my_token/发送POST请求。