telegram chat bot using flask

Solutions on MaxInterview for telegram chat bot using flask by the best coders in the world

showing results for - "telegram chat bot using flask"
Amir
28 Jul 2016
1import re
2from flask import Flask, request
3import telegram
4from telebot.credentials import bot_token, bot_user_name,URL
5
6
7global bot
8global TOKEN
9TOKEN = bot_token
10bot = telegram.Bot(token=TOKEN)
11
12app = Flask(__name__)
13
14@app.route('/{}'.format(TOKEN), methods=['POST'])
15def respond():
16   # retrieve the message in JSON and then transform it to Telegram object
17   update = telegram.Update.de_json(request.get_json(force=True), bot)
18
19   chat_id = update.message.chat.id
20   msg_id = update.message.message_id
21
22   # Telegram understands UTF-8, so encode text for unicode compatibility
23   text = update.message.text.encode('utf-8').decode()
24   # for debugging purposes only
25   print("got text message :", text)
26   # the first time you chat with the bot AKA the welcoming message
27   if text == "/start":
28       # print the welcoming message
29       bot_welcome = """
30       Welcome to coolAvatar bot, the bot is using the service from http://avatars.adorable.io/ to generate cool looking avatars based on the name you enter so please enter a name and the bot will reply with an avatar for your name.
31       """
32       # send the welcoming message
33       bot.sendMessage(chat_id=chat_id, text=bot_welcome, reply_to_message_id=msg_id)
34
35
36   else:
37       try:
38           # clear the message we got from any non alphabets
39           text = re.sub(r"\W", "_", text)
40           # create the api link for the avatar based on http://avatars.adorable.io/
41           url = "https://api.adorable.io/avatars/285/{}.png".format(text.strip())
42           # reply with a photo to the name the user sent,
43           # note that you can send photos by url and telegram will fetch it for you
44           bot.sendPhoto(chat_id=chat_id, photo=url, reply_to_message_id=msg_id)
45       except Exception:
46           # if things went wrong
47           bot.sendMessage(chat_id=chat_id, text="There was a problem in the name you used, please enter different name", reply_to_message_id=msg_id)
48
49   return 'ok'
50
51@app.route('/set_webhook', methods=['GET', 'POST'])
52def set_webhook():
53   s = bot.setWebhook('{URL}{HOOK}'.format(URL=URL, HOOK=TOKEN))
54   if s:
55       return "webhook setup ok"
56   else:
57       return "webhook setup failed"
58
59@app.route('/')
60def index():
61   return '.'
62
63
64if __name__ == '__main__':
65   app.run(threaded=True)
66
queries leading to this page
how to create a telegram bot in flaskflask telegram bot webhooktelegram bot tutorialhow to add logo in telegram bot pythonhow to write telegram bot on flaskpython flask telegram botflask python telegram botflask store telegram bot datahow to make telegram bot using flasktelegram bot python flaskconnect telegram bot to flasktelegram bot flask 3ftelegram bot with python and flaskflask and telegram bottelegram chat bot using flasktelegram bot flask set webhooktelegram bot with flask projecttelegram bot using python flaskflask telegram bot routedevelop your custom telegram bot for youtelegram bot flaskpython telegram bot 2b flasktelegram pythontelegram site chat widgettelegram bot pythonflask telegram bottelegram bot with flaskpython telegram bot tutorial to send messagepython telegram bot flask examplehow to make a telegram bot wait for a response from a user and store the response in a database using the python telegram botuser bot telegram flaskflask set webhook telegram bothow to add telegram bot in flasktelegram bot using flaskflask chat botchatbot on telegram using python flaskpython telegram bot webhook flaskchatterbot flask telegramtelegram bot flask documentationtelegram bot command pythonhow to design advanced telegram bot pythonbuild telegram bots with flaskpython telegram bot flaskreact and flask chat bottelegram bot webhook flaskbot telegram along with flask apphow to make a telegram botflask telegram bot main functiontelegram bot based on python flasktelegram chat bot using flask