Telegram+python+flask+PyTelegramBotApi+pythonanywhere+webhook?

Good afternoon.
Clambered all sites, but the feeling that the Chukchi fool has become so as much as possible, I decided to ask.
The problem of trifling turned to me almost a problem of the century.

The bottom line:
write a bot for Telegram in Python with subsequent placement on pythonanywhere (because it is convenient, not the point)

I have a problem with WebHook'om
itself hang webhook is:

https://api.telegram.org/bot<token>/setWebhook?url=http://username.pythonanywhere.com/</token>


BUT... I have troubles with the Flask.
How to fix the code? I do not understand how flask to make friends (so to speak) with telebot to hang webhook. Watched the documentation PyTelegramBotApi as it is too tricky, you can also easier. Or am I mistaken?

import telebot
from flask import Flask
import config

token = config.token
bot = telebot.TeleBot(token)

app = Flask(__name__)
@app.route('/')
#I KNOW THAT'S NOT ENOUGH, BUT HOW IS IT TO FINISH??

@bot.message_handler(commands=['start'])
def handle_start(message):
 bot.send_message(message.from_user.id, 'HELLO!')


if __name__ == '__main__':
 app.run()
June 10th 19 at 14:59
4 answers
June 10th 19 at 15:01
For PyTelegramBotApi works as follows:

from flask import Flask, request
import telebot
from telebot import types
import time

secret = "GUID"
bot = telebot.TeleBot('TOKEN_TELEGRAM', threaded=False)

bot.remove_webhook()
time.sleep(1)
bot.set_webhook(url="https://YOURNAME.pythonanywhere.com/{}".format(secret))

app = Flask(__name__)

@app.route('/{}'.format(secret), methods=["POST"])
def webhook():
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
print("Message")
 return "ok", 200


@bot.message_handler(commands=['start', 'help'])
def startCommand(message):
 bot.send_message(message.chat.id, 'Hi *' + message.chat.first_name + '*!' , parse_mode='Markdown', reply_markup=types.ReplyKeyboardRemove())


checked on the paid account
Why remove and put webhook again? - tyrel commented on June 10th 19 at 15:04
June 10th 19 at 15:03
The address of the url should contain some kind of random sequence that only the server just wish more people would know where to shoot an update
Yes it is clear that more can and such a thing to attach. The question is not that. What code wrong? That should be corrected? in this flask'e - tyrel commented on June 10th 19 at 15:06
June 10th 19 at 15:05
here kaeso ripped
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])


json_string pull from the flask request object
June 10th 19 at 15:07
honestly, zabadal me this question. who cares. use another library called telepot.

# -*- coding: utf8 -*-
from flask import Flask, request
import telepot
import urllib3

proxy_url = "http://proxy.server:3128"
telepot.api._pools = {
 'default': urllib3.ProxyManager(proxy_url=proxy_url, num_pools=3, maxsize=10, retries=False, timeout=30),
}
telepot.api._onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=proxy_url, num_pools=1, maxsize=1, retries=False, timeout=30))

secret = "SECRET_KEY"
bot = telepot.Bot(TOKEN)
bot.setWebhook("https://USER_NAME.pythonanywhere.com/{}".format(secret), max_connections=1)

app = Flask(__name__)

@app.route('/{}'.format(secret), methods=["POST"])
def telegram_webhook():
 update = request.get_json()
 if "message" in update:
 text = update["message"]["text"]
 chat_id = update["message"]["chat"]["id"]
 if text == "/start":
 bot.sendMessage(chat_id, 'did You call the command Test')
else:
 bot.sendMessage(chat_id, 'You write some letters')
 return "OK"


this code just keep updating pythonanywhare and everything works.

Find more questions by tags TelegramPython