forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegramBot.py
More file actions
69 lines (58 loc) · 2.27 KB
/
TelegramBot.py
File metadata and controls
69 lines (58 loc) · 2.27 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
from telegram.ext import Updater, InlineQueryHandler, CommandHandler, MessageHandler,Filters
import requests
import re
#function to get contents of url(public api) using requests
def gett():
contents = requests.get('https://random.dog/woof.json').json()
url = contents['url']
return url
#function to check allowed extension
def image_urll():
extension = ['jpg','jpeg','png']
ext = ''
while ext not in extension:
url = gett()
ext = re.search("([^.]*)$",url).group(1).lower()
return url
def get():
contents = requests.get('https://xkcd.com/info.0.json').json()
img = contents['img']
return img
def image_url():
extension = ['jpg','jpeg','png']
ext = ''
while ext not in extension:
img = get()
ext = re.search("([^.]*)$",img).group(1).lower()
return img
#function to display dog picture
def dog(update, context):
url = image_urll()
context.bot.send_photo(chat_id=update.effective_chat.id, photo = url)
#function to display meme
def meme(update, context):
img = image_url()
context.bot.send_photo(chat_id=update.effective_chat.id, photo = img)
#Welcome message will be displayed when /hi command is sent
def hi(update, context):
context.bot.send_message(chat_id = update.effective_chat.id, text="Hi! I am telebot...How is it going.")
#tongue-twister will be displayed when /play command is sent
def play(update, context):
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.")
#Displays message when incorrect command is sent
def unknown(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Please type the right command.")
#your token token has to be provided in <<YOUR-TOKEN>> place
def main():
upd = Updater('<<YOUR-TOKEN>>', use_context=True)
disp = upd.dispatcher
unknown_handler = MessageHandler(Filters.command, unknown)
hi_handler = CommandHandler('hi', hi)
disp.add_handler(CommandHandler('meme', meme))
disp.add_handler(CommandHandler('play', play))
disp.add_handler(CommandHandler('dog',dog))
disp.add_handler(hi_handler)
disp.add_handler(unknown_handler)
upd.start_polling()
upd.idle()
main()