Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, request
- import logging
- import Scraper
- from credentials import bot_token, bot_user_name,URL
- import telegram
- from telegram.ext import Dispatcher, CommandHandler, MessageHandler, Filters
- app = Flask(__name__)
- application = app # hosting requires application in passenger_wsgi
- TOKEN = bot_token
- bot = telegram.Bot(token=TOKEN)
- def start(bot, update): #Example of callback function for dispatcher when using flask to handle update
- update.message.reply_text('Hi!')
- def chatid(bot, update): #Example of callback function for dispatcher when using flask to handle update
- update.message.reply_text('Chat id is {}'.format(update.message.chat_id))
- #def start(update, context):
- # context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
- #context cannot be used maybe because need to create updater with use_context=True. Flask is already handling update.
- #have to use the above 2 callback functions if you are using flask to handle update
- #The above callback function is shown in most example of the website.
- def setup(token): #need to add this function if you are using flask to handle update
- # Create bot, update queue and dispatcher instances
- bot = telegram.Bot(token)
- dispatcher = Dispatcher(bot, None, workers=0)
- ##### Register handlers here #####
- start_handler = CommandHandler('start', start)
- dispatcher.add_handler(start_handler)
- dispatcher.add_handler(CommandHandler('chatid', chatid))
- return dispatcher
- @app.route("/{}".format(TOKEN), methods=["POST","GET"])
- def respond():
- if request.method == "GET":
- return "Working Webhook"
- else:
- try:
- dispatcher=setup(TOKEN) #need to add this line if you are using flask to handle update
- update = telegram.Update.de_json(request.get_json(force=True), bot)
- chat_id = update.message.chat.id
- msg_id = update.message.message_id
- text = update.message.text.encode("utf-8").decode()
- dispatcher.process_update(update) #need to add this line if you are using flask to handle update
- return Response('ok', status=200)
- finally:
- pass
- return "ok. Wrong Request or Error"
- if __name__ == "__main__":
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement