-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
94 lines (73 loc) · 3.13 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 1. Enable Logging
# we want to know about the activities(warning,errors) in a systematic manner
# thats why we need logging
import logging
from flask import Flask, request
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, Dispatcher
from telegram import Bot, Update, ReplyKeyboardMarkup
from utils import get_reply, fetch_news, topics_keyboard
# logger object will help us create the logs in this program
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
TOKEN = "1701925941:AAHNOV5z5z6S100tno5eqO1fMnytLfnHq74"
app=Flask(__name__)
@app.route('/')
def index():
return "Hello!"
@app.route(f'/{TOKEN}', methods=['GET', 'POST'])
def webhook():
"""webhook view which receives 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"
# CommandHandlers
# 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 start(update, context):
print(update)
author=update.message.from_user.first_name
reply="Hi! {}".format(author)
context.bot.sendMessage(chat_id=update.message.chat_id, text=reply)
def _help(update, context):
help_text="Hey! This is a help text."
context.bot.send_message(chat_id=update.message.chat_id, text=help_text)
def news(update, context):
context.bot.send_message(chat_id=update.message.chat_id, text="Choose a catagory",
reply_markup=ReplyKeyboardMarkup(keyboard=topics_keyboard, one_time_keyboard=True))
def reply_text(update, context):
"""callback function for text message handler"""
intent, reply = get_reply(update.message.text, update.message.chat_id)
if intent == "get_news":
articles = fetch_news(reply)
for article in articles:
context.bot.send_message(chat_id=update.message.chat_id, text=article['link'])
else:
context.bot.send_message(chat_id=update.message.chat_id, text=reply)
def echo_sticker(update, context):
context.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)
# this will try to recieve updates for the bot
# this will keep on handling the updates
# def main():
bot = Bot(TOKEN)
try:
bot.set_webhook("https://ancient-caverns-72036.herokuapp.com/" + TOKEN)
time.sleep(5)
except Exception as e:
print(e)
dp = Dispatcher(bot, None)
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", _help))
dp.add_handler(CommandHandler("news", news))
dp.add_handler(MessageHandler(Filters.text, reply_text))
dp.add_handler(MessageHandler(Filters.sticker, echo_sticker))
dp.add_error_handler(error)
if __name__=="__main__":
app.run(port=8443)