Skip to content

Instantly share code, notes, and snippets.

@kostyakoz
Created January 7, 2017 18:01
Show Gist options
  • Save kostyakoz/dcecd1663ba92045ab9c60a4879a7b48 to your computer and use it in GitHub Desktop.
Save kostyakoz/dcecd1663ba92045ab9c60a4879a7b48 to your computer and use it in GitHub Desktop.
Telegram Bot for books reading
import telebot
from telebot import types
TOKEN = ''
bot = telebot.TeleBot(TOKEN)
with open('./SH.txt', 'r') as file:
BOOK = file.read() # открываем книгу и записываем её в BOOK
def pages_keyboard(start, stop):
"""Формируем Inline-кнопки для перехода по страницам.
"""
keyboard = types.InlineKeyboardMarkup()
btns = []
if start > 0: btns.append(types.InlineKeyboardButton(
text='⬅', callback_data='to_{}'.format(start - 700)))
if stop < len(BOOK): btns.append(types.InlineKeyboardButton(
text='➡', callback_data='to_{}'.format(stop)))
keyboard.add(*btns)
return keyboard # возвращаем объект клавиатуры
@bot.message_handler(commands=['start'])
def start(m):
"""Отвечаем на команду /start
"""
bot.send_message(m.chat.id, BOOK[:700], parse_mode='Markdown',
reply_markup=pages_keyboard(0, 700))
@bot.callback_query_handler(func=lambda c: c.data)
def pages(c):
"""Редактируем сообщение каждый раз, когда пользователь переходит по
страницам.
"""
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id,
text=BOOK[int(c.data[3:]):int(c.data[3:]) + 700],
parse_mode='Markdown',
reply_markup=pages_keyboard(int(c.data[3:]),
int(c.data[3:]) + 700))
bot.polling()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment