Skip to content

Instantly share code, notes, and snippets.

@kachaMukabe
Created August 16, 2020 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kachaMukabe/09f80f5ac7d9e59edea128b86fcc11f8 to your computer and use it in GitHub Desktop.
Save kachaMukabe/09f80f5ac7d9e59edea128b86fcc11f8 to your computer and use it in GitHub Desktop.
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, InlineQueryHandler
from telegram import InlineQueryResultArticle, InputTextMessageContent
import json
import requests
import os
updater = Updater(token=os.eviron["TELEGRAM_TOKEN"], use_context=True)
dispatcher = updater.dispatcher
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
API_URL = 'http://localhost:5000'
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Talk to me")
def list_books(update, context):
response = requests.get(f'{API_URL}/books')
# books = 'Book one\nBook tow\nBook tres\nBokk dues'
books = response.json()
book_response = '\n'.join(json.dumps(book) for book in books)
context.bot.send_message(chat_id=update.effective_chat.id, text=book_response)
def search(update, context):
search_term = ' '.join(context.args)
response = requests.get(f'{API_URL}/search/{search_term}')
context.bot.send_message(chat_id=update.effective_chat.id, text=response.json())
def list_overdue(update, context):
response = requests.get(f'{API_URL}/overdue')
books = response.json()
book_response = '\n'.join(json.dumps(book) for book in books)
context.bot.send_message(chat_id=update.effective_chat.id, text=book_response)
def book_detail(update, context):
book_id = context.args[0]
response = requests.get(f'{API_URL}/bookdetail/{book_id}')
book = response.json()
context.bot.send_message(chat_id=update.effective_chat.id, text=book)
def echo(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)
start_handler = CommandHandler('hello', start)
search_handler = CommandHandler('search', search)
list_books_handler = CommandHandler('listbooks', list_books)
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
book_detail_handler = CommandHandler('bookdetail', book_detail)
overdue_handler = CommandHandler('listoverdue', list_overdue)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(list_books_handler)
dispatcher.add_handler(search_handler)
dispatcher.add_handler(book_detail_handler)
dispatcher.add_handler(overdue_handler)
updater.start_polling()
updater.idle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment