-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
executable file
·57 lines (44 loc) · 2.45 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
#!/usr/bin/env python
from telegram.ext import Updater, MessageHandler, CommandHandler, InlineQueryHandler, Filters
from telegram import InlineQueryResultArticle, InputTextMessageContent, ParseMode
from parser import Parser
class TelegramBot(object):
def listener(self, bot, update):
id = update.message.chat_id
mensaje = update.message.text
print("ID: " + str(id) + " MESSAGE: " + mensaje)
def start(self, bot, update):
self.listener(bot, update)
bot.sendMessage(chat_id=update.message.chat_id, text='Welcome, you can use the following commands:')
bot.sendMessage(chat_id=update.message.chat_id, text='/book Today\'s book info.', parse_mode=ParseMode.HTML)
bot.sendMessage(chat_id=update.message.chat_id, text='/link Link to download the book.', parse_mode=ParseMode.HTML)
bot.sendMessage(chat_id=update.message.chat_id, text='/help Commands available.', parse_mode=ParseMode.HTML)
def help(self, bot, update):
self.listener(bot, update)
bot.sendMessage(chat_id=update.message.chat_id, text='/book Today\'s book info.', parse_mode=ParseMode.HTML)
bot.sendMessage(chat_id=update.message.chat_id, text='/link Link to download the book.', parse_mode=ParseMode.HTML)
bot.sendMessage(chat_id=update.message.chat_id, text='/help Commands available.', parse_mode=ParseMode.HTML)
def book(self, bot, update):
self.listener(bot, update)
for msg in self.parser.get_book() :
bot.sendMessage(chat_id=update.message.chat_id, text=msg, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
bot.sendPhoto(chat_id=update.message.chat_id, photo=self.parser.get_image())
def link(self, bot, update):
self.listener(bot, update)
bot.sendMessage(chat_id=update.message.chat_id,
text='Download today\'s free book <a href="https://www.packtpub.com/packt/offers/free-learning/">HERE</a>.', parse_mode=ParseMode.HTML, disable_web_page_preview=True)
def run(self):
updater = Updater('558256017:AAGEKKqDDV4vRw2O45N8es1UVX6GBdvOr1s')
self.parser = Parser()
dispatcher = updater.dispatcher
listener_handler = MessageHandler(Filters.text, self.listener)
dispatcher.add_handler(listener_handler)
dispatcher.add_handler(CommandHandler("start", self.start))
dispatcher.add_handler(CommandHandler("help", self.help))
dispatcher.add_handler(CommandHandler("book", self.book))
dispatcher.add_handler(CommandHandler("link", self.link))
updater.start_polling()
updater.idle()
if __name__ == "__main__":
telegramBot = TelegramBot()
telegramBot.run()