-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfact.py
44 lines (38 loc) · 1.3 KB
/
fact.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
#
# TELEGRAM BOT
#
# Name: Nova
# Username: Nova_X1_Bot
#
# - Command: /fact
# - Description:
# Send random fact. Subjects: date, math, trivia and year.
#
# By Jose Acevedo
# Copyright 2016.
import random
import unirest
def handleFact(msg, chatID, bot):
# Choose from 4 possible categories
factType = random.choice(["trivia","math","date","year"])
# Fetch fact in plain text
response = unirest.get("https://numbersapi.p.mashape.com/random/"+factType+"?fragment=false&json=true",headers={"X-Mashape-Key":"DuskHHYl5DmshhsjvJ4LaVXZinfpp1KnC92jsndIqrz6pC0CDa","Accept": "text/plain"}).body
# Handle "year" type facts
if response["type"] == "year":
if "year" in response:
fact = "Year: " + str(response["year"]) + "\n" + response["text"].title() + "."
else:
fact = response["text"].title() + "."
# Handle "math" type facts
elif response["type"] == "math":
fact = response["text"].title() + " is " + str(response["number"]) + "."
# Handle "trivia" type facts
elif response["type"] == "trivia":
fact = response["text"].title() + " is " + str(response["number"]) + "."
# Handle "date" type facts
else:
if "year" in response:
fact = "Year: " + str(response["year"]) + "\n" + response["text"].title() + "."
else:
fact = response["text"].title() + "."
bot.sendMessage(chatID, fact)