|
| 1 | +from telegram.ext import Updater, InlineQueryHandler, CommandHandler, MessageHandler,Filters |
| 2 | +import requests |
| 3 | +import re |
| 4 | + |
| 5 | +#function to get contents of url(public api) using requests |
| 6 | +def gett(): |
| 7 | + contents = requests.get('https://random.dog/woof.json').json() |
| 8 | + url = contents['url'] |
| 9 | + return url |
| 10 | + |
| 11 | +#function to check allowed extension |
| 12 | +def image_urll(): |
| 13 | + extension = ['jpg','jpeg','png'] |
| 14 | + ext = '' |
| 15 | + while ext not in extension: |
| 16 | + url = gett() |
| 17 | + ext = re.search("([^.]*)$",url).group(1).lower() |
| 18 | + return url |
| 19 | + |
| 20 | +def get(): |
| 21 | + contents = requests.get('https://xkcd.com/info.0.json').json() |
| 22 | + img = contents['img'] |
| 23 | + return img |
| 24 | + |
| 25 | +def image_url(): |
| 26 | + extension = ['jpg','jpeg','png'] |
| 27 | + ext = '' |
| 28 | + while ext not in extension: |
| 29 | + img = get() |
| 30 | + ext = re.search("([^.]*)$",img).group(1).lower() |
| 31 | + return img |
| 32 | + |
| 33 | +#function to display dog picture |
| 34 | +def dog(update, context): |
| 35 | + url = image_urll() |
| 36 | + context.bot.send_photo(chat_id=update.effective_chat.id, photo = url) |
| 37 | + |
| 38 | +#function to display meme |
| 39 | +def meme(update, context): |
| 40 | + img = image_url() |
| 41 | + context.bot.send_photo(chat_id=update.effective_chat.id, photo = img) |
| 42 | + |
| 43 | +#Welcome message will be displayed when /hi command is sent |
| 44 | +def hi(update, context): |
| 45 | + context.bot.send_message(chat_id = update.effective_chat.id, text="Hi! I am telebot...How is it going.") |
| 46 | + |
| 47 | +#tongue-twister will be displayed when /play command is sent |
| 48 | +def play(update, context): |
| 49 | + context.bot.send_message(chat_id = update.effective_chat.id, text="Let's have fun!Repeat this tongue twister 5 times:She sells seashells by the seashore.") |
| 50 | + |
| 51 | +#Displays message when incorrect command is sent |
| 52 | +def unknown(update, context): |
| 53 | + context.bot.send_message(chat_id=update.effective_chat.id, text="Please type the right command.") |
| 54 | + |
| 55 | +#your token token has to be provided in <<YOUR-TOKEN>> place |
| 56 | +def main(): |
| 57 | + upd = Updater('<<YOUR-TOKEN>>', use_context=True) |
| 58 | + disp = upd.dispatcher |
| 59 | + unknown_handler = MessageHandler(Filters.command, unknown) |
| 60 | + hi_handler = CommandHandler('hi', hi) |
| 61 | + disp.add_handler(CommandHandler('meme', meme)) |
| 62 | + disp.add_handler(CommandHandler('play', play)) |
| 63 | + disp.add_handler(CommandHandler('dog',dog)) |
| 64 | + disp.add_handler(hi_handler) |
| 65 | + disp.add_handler(unknown_handler) |
| 66 | + upd.start_polling() |
| 67 | + upd.idle() |
| 68 | + |
| 69 | +main() |
0 commit comments