-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
48 lines (43 loc) · 1.76 KB
/
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
"""Parse message and return the proper bot response"""
import random
import requests
#based on command respond with bot
def bot(message):
"""Parse message and return the proper bot response"""
spltmsg = message.split()
cmd = spltmsg[1]
if cmd == 'about': # description of bot
return ('Hi, I am BobbyBot. I am a pretty fun guy. If there is something you need from' +
' me let me know. To find out what I am capable of type !! help')
if cmd == 'help': # command list
ret = "!! about - learn about me<br>"
ret += "!! help - shows this screen<br>"
ret += "!! funtranslate {message} - translate message to {language}<br>"
ret += "!! flip - flip a coin<br>"
ret += "!! bitcoin - I will tell you bitcoins price"
return ret
if cmd == 'flip': # flip a coin
coin = random.getrandbits(1)
if coin == 1:
return 'The coin landed HEADS up'
return 'The coin landed TAILS up'
if cmd == 'funtranslate': # translate to piglatin
url = "https://api.funtranslations.com/translate/pig-latin.json?text="
for i in range(2,len(spltmsg)):
url+=spltmsg[i] + '+'
req=requests.get(url)
data = req.json()
try:
ret = data["contents"]["translated"][:-2]
except:
ret = "Sorry the limit for translations has been reached"
return ret
if cmd == 'bitcoin': # message the price of bitcoin
url = "https://api.coindesk.com/v1/bpi/currentprice.json"
req=requests.get(url)
data = req.json()
return ("1 bitcoin is currently worth "
+ data["bpi"]["USD"]["symbol"] + ""
+ data["bpi"]["USD"]["rate"])
# command doesn't exist
return "I don't know how to do that"