-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoggo_bot.py
59 lines (51 loc) · 1.75 KB
/
doggo_bot.py
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
from telegram.ext import Updater, InlineQueryHandler, CommandHandler
import requests
import re
import json
def get_url_dog():
contents = requests.get('https://random.dog/woof.json').json() #API dog
url_dog = contents['url']
return url_dog
def get_image_url_dog():
allowed_extension = ['jpg','jpeg','png']
file_extension = ''
while file_extension not in allowed_extension:
url_dog = get_url_dog()
file_extension = re.search("([^.]*)$",url_dog).group(1).lower()
return url_dog
def woof(bot, update): #request for dog image
url_dog = get_image_url_dog()
chat_id = update.message.chat_id
bot.send_photo(chat_id=chat_id, photo=url_dog)
def get_url_cat(): #API cat from TheCatAPI
urlCAT = 'https://api.thecatapi.com/v1/images/search?format=json'
payload = {}
headers = {
'Content-Type': 'application/json',
'x-api-key': '06d76875-b54b-4bab-b2ec-e561e6bdf3e1'
}
response = requests.request('GET', urlCAT)
data = json.loads(response.text) #list
for i in data:
url_cat = i["url"] #dict
return url_cat
def get_image_url_cat():
allowed_extension = ['jpg','jpeg','png']
file_extension = ''
while file_extension not in allowed_extension:
url_cat = get_url_cat()
file_extension = re.search("([^.]*)$",url_cat).group(1).lower()
return url_cat
def purr(bot, update): #request for cat image
url_cat = get_url_cat()
chat_id = update.message.chat_id
bot.send_photo(chat_id=chat_id, photo=url_cat)
def main():
updater = Updater('723455293:AAGckXg1oR6k61GBWVEJeMM9lk3vjnDBdHY')
dp = updater.dispatcher
dp.add_handler(CommandHandler('woof', woof))
dp.add_handler(CommandHandler('purr', purr))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()