import logging
from flask import Flask
from flask import request
from telegram.ext import Updater,CommandHandler,MessageHandler,Filters,Dispatcher
from telegram import Bot,Update
from utils import get_reply,fetch_news

#enable logging
logging.basicConfig(format='%(asctime)s-%(name)s-%(levelname)s - %(message)s',level=logging.INFO)
logger = logging.getLogger(__name__)

TOKEN = "973000823:AAH0P-8kx-ca3iSAo5FRu2rutMKRAI-coxQ"

app = Flask(__name__)

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

@app.route('/973000823:AAH0P-8kx-ca3iSAo5FRu2rutMKRAI-coxQ', methods=['GET','POST'])

def webhook():
	#create an update object from telegram
	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
    #msg= update.message.text
    reply = "Hi! {}".format(author)
    bot.send_message(chat_id=update.message.chat_id,text=reply)

def _help(bot,update):
	help_txt="Hey! This is a help text."
	bot.send_message(chat_id=update.message.chat_id,text=help_txt)

def reply_text(bot,update):
	intent, reply=get_reply(update.message.text,update.message.chat_id)
	if intent=="get_news_intent":
		articles = fetch_news(reply)
		for article in articles:
			bot.send_message(chat_id=update.message.chat_id,text=article['link'])
	else :
		bot.send_message(chat_id=update.message.chat_id,text=reply)

def echo_sticker(bot,update):
	#reply=update.message.txt
	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)

if __name__ == "__main__":
	bot = Bot(TOKEN)
	bot.set_webhook("https://6...content-available-to-author-only...k.io/"+ TOKEN)

	dp = Dispatcher(bot,None)

	dp.add_handler(CommandHandler("start",start))
	dp.add_handler(CommandHandler("help",_help))
	dp.add_handler(MessageHandler(Filters.text,reply_text))
	dp.add_handler(MessageHandler(Filters.sticker,echo_sticker))
	dp.add_error_handler(error)
	app.run(port=8443)





# your code goes here