-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquotes.py
43 lines (36 loc) · 1.18 KB
/
quotes.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
import os
import requests
import urllib
from dotenv import load_dotenv
load_dotenv()
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
API_KEY_THEYSAIDSO = os.getenv("API_KEY_THEYSAIDSO")
URL_QUOTE = "https://quotes.rest/qod"
HEADERS = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-TheySaidSo-Api-Secret': API_KEY_THEYSAIDSO
}
def getQuote():
r = requests.get(url = URL_QUOTE, headers = HEADERS)
data = r.json()
message = ''
if 'contents' in data:
quote = data['contents']['quotes'][0]['quote']
author = data['contents']['quotes'][0]['author']
permalink = data['contents']['quotes'][0]['permalink']
message = f"\"{quote}\"\n- {author}"
elif 'error' in data:
print("Error: " + data['error']['message'])
elif 'message' in data:
print("Error: " + data['message'])
return message
def sendMessage(query):
r = requests.get(url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage?chat_id={TELEGRAM_CHAT_ID}&text={urllib.parse.quote(query)}", headers = HEADERS)
def main():
quote = getQuote()
if (quote != ''):
sendMessage(quote)
if __name__ == "__main__":
main()