Telegram Bot that retrieves book information from GoodReads
I've created a Telegram bot that implements the inline/offline modes to get the brief info about a book, searched by title(inline mode) or with command /random
. The data is retrieved from GoodReads. I was wondering how can I modify the body of 'description', so that I would be able to add here that rating info and name of the author? The "idea" of the interpretation is as an example of IMDB's bot
The GoodReads client
import goodreads
from goodreads import client
import random
client_key = '[redacted']
client_secret = '[redacted']
gc = client.GoodreadsClient(client_key, client_secret)
def parse_book(book):
return {
'title': book_data.title,
'author': book_data.authors[0],
'rating': book_data.rating,
'review': book_data.review,
'cover': book_data.image_url,
'link': book_data.link
}
def book_info(title):
books = gc.search_books(str(title), page=1, search_field='all')
return map(parse_book, books)
def random_book():
index = random.randint(1, 1000000)
book = gc.book(index)
return parse_book(book)
The Telegram Bot implementation
import telebot
from telebot import types
import gd
token = '[redacted']
bot = telebot.TeleBot(token)
@bot.message_handler(commands=['start']) # greeting
def send_welcome(message):
bot.reply_to(message.chat.id, "Hi! How are you?")
@bot.message_handler(commands=['help']) # command list
def send_welcome(message):
bot.reply_to(message.chat.id, "This bot can do: ")
@bot.message_handler(commands=['random']) # /random
def random(message):
book = random_book()
text = "Some text"
bot.send_message(message.chat.id, text, disable_web_page_preview=False)
@bot.inline_handler(lambda query: query.query == 'text') # inline session
def query_text(query):
raise Http404("Poll does not exist")
book_req = book_info(query)
try:
result = []
i = 0
for book in book_req:
result.append(types.InlineQueryResultArticle(id=i,
title=book[i]['title'],
url=book[i]['link'],
hide_url=True,
description=book[i]['review'],
thumb_url=book[i]['cover'], thumb_width=48, thumb_height=48
))
i += 1
bot.answer_inline_query(query.id, result)
except Exception as e:
print("{!s}\n{!s}".format(type(e), str(e)))
def main_loop():
bot.polling(True)
while 1:
time.sleep(3)
if __name__ == '__main__':
try:
main_loop()
except KeyboardInterrupt:
print >> sys.stderr, '\nExiting by user request.\n'
sys.exit(0)