Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

telegram bot webhook python

import re
from flask import Flask, request
import telegram
from telebot.credentials import bot_token, bot_user_name,URL


global bot
global TOKEN
TOKEN = bot_token
bot = telegram.Bot(token=TOKEN)

app = Flask(__name__)

@app.route('/{}'.format(TOKEN), methods=['POST'])
def respond():
   # retrieve the message in JSON and then transform it to Telegram object
   update = telegram.Update.de_json(request.get_json(force=True), bot)

   chat_id = update.message.chat.id
   msg_id = update.message.message_id

   # Telegram understands UTF-8, so encode text for unicode compatibility
   text = update.message.text.encode('utf-8').decode()
   # for debugging purposes only
   print("got text message :", text)
   # the first time you chat with the bot AKA the welcoming message
   if text == "/start":
       # print the welcoming message
       bot_welcome = """
       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.
       """
       # send the welcoming message
       bot.sendMessage(chat_id=chat_id, text=bot_welcome, reply_to_message_id=msg_id)


   else:
       try:
           # clear the message we got from any non alphabets
           text = re.sub(r"W", "_", text)
           # create the api link for the avatar based on http://avatars.adorable.io/
           url = "https://api.adorable.io/avatars/285/{}.png".format(text.strip())
           # reply with a photo to the name the user sent,
           # note that you can send photos by url and telegram will fetch it for you
           bot.sendPhoto(chat_id=chat_id, photo=url, reply_to_message_id=msg_id)
       except Exception:
           # if things went wrong
           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)

   return 'ok'

@app.route('/set_webhook', methods=['GET', 'POST'])
def set_webhook():
   s = bot.setWebhook('{URL}{HOOK}'.format(URL=URL, HOOK=TOKEN))
   if s:
       return "webhook setup ok"
   else:
       return "webhook setup failed"

@app.route('/')
def index():
   return '.'


if __name__ == '__main__':
   app.run(threaded=True)
Comment

PREVIOUS NEXT
Code Example
Python :: numpy.random.choice 
Python :: how to download packages using pip 
Python :: get input on same line python 
Python :: sort and remove duplicates list python 
Python :: pandas number format 
Python :: regex for digits python 
Python :: python euclidean distance 
Python :: pd.datafram 
Python :: merge two query sets django 
Python :: python repet x time 
Python :: how to install arcade in python 
Python :: get the name of all files in a computer in python 
Python :: python change directory to previous 
Python :: continue statement python 
Python :: days to int 
Python :: higlight words in python 
Python :: python turn positive into negative 
Python :: res.send is not a function 
Python :: create django project 
Python :: stutter function in python 
Python :: python string ends with 
Python :: how to capitalize first letter in python in list using list comprehension 
Python :: break while loop python 
Python :: Setting spacing between ticks in matplotlib 
Python :: django data from many to many field in template 
Python :: 2d arrays using python numpy 
Python :: python list contain list 
Python :: image blur in python 
Python :: python set workdir 
Python :: bitwise operators python 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =