Created
July 13, 2020 13:17
-
-
Save NeyoxDrago/edde0d03102f67961d8e456ee3d42a14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from flask import Flask, request | |
from telegram.ext import Updater, CommandHandler , MessageHandler , Filters, Dispatcher | |
from telegram import Bot , Update | |
#enable logging | |
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s %(message)s', | |
level= logging.INFO) | |
logger = logging.getLogger(__name__) | |
TOKEN = "1363998583:AAHSEmA8H0-YDELz3CEABNmcUrYhiaMtsOo" | |
#webhook----------------- | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
return "hello!" | |
@app.route(f'/{TOKEN}' , methods=['GET','POST']) | |
def webhook(): | |
'''webhook view which recieves updates from telegram''' | |
# create update object from json-format request data | |
update = Update.de_json(request.get_json(),bot) | |
#process update | |
dp.process_update(update) | |
return "ok" | |
#------------------------ | |
def start(bot,update): | |
print(update) | |
author = update.message.from_user.first_name | |
reply = "Hi! {}".format(author) | |
bot.send_message(chat_id = update.message.chat_id, text=reply) | |
def _help(bot,update): | |
help_text = "hey! this is help" | |
bot.send_message(chat_id=update.message.chat_id, text=help_text) | |
def echo_text(bot,update): | |
reply = update.message.text | |
bot.send_message(chat_id = update.message.chat_id, text=reply) | |
def echo_sticker(bot,update): | |
bot.send_sticker(chat_id = update.message.chat_id, | |
sticker= update.message.sticker.file_id) | |
def error(bot,update): | |
logger.error("update '%s' caused error '%s'",update , update.error) | |
##def main(): | |
## # updater = Updater(TOKEN) for echo bot | |
## # dp = updater.dispatcher | |
## # updater.start_polling() | |
## # logger.info("Started polling") #for echo bot we used polling | |
## # updater.idle() | |
if __name__ == "__main__": | |
bot = Bot(TOKEN) | |
bot.set_webhook("https://3eb68f055fe1.ngrok.io/" + TOKEN) | |
dp = Dispatcher(bot , None) | |
dp.add_handler(CommandHandler("start",start)) | |
dp.add_handler(CommandHandler("help",_help)) | |
dp.add_handler(MessageHandler(Filters.text,echo_text)) | |
dp.add_handler(MessageHandler(Filters.sticker,echo_sticker)) | |
dp.add_error_handler(error) | |
app.run(port=8443) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment