-
Notifications
You must be signed in to change notification settings - Fork 0
/
mission_Spark_bot_super.py
146 lines (98 loc) · 4.94 KB
/
mission_Spark_bot_super.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from flask import Flask, request
import requests
import json
# Global variables
baseurl = "https://api.ciscospark.com/v1"
bot_auth_token = "NzJkMzE0ZjMtOWJjZS00Y2JkLWIyZjAtNDI4MjA0MjA0ZWIxNDgxNjY4NjMtZTlk"
headers = {"Content-Type": "application/json",
"accept": "application/json",
"Authorization": "Bearer %s" % bot_auth_token
}
app = Flask(__name__)
def get_message(data):
mess_id = data['id']
#Python has powerful string and text manipulation.
# I recommend checking out https://learnpythonthehardway.org/book/ex6.html
# for more info on this.
mess_api = "/messages/%s" % mess_id
mess_url = baseurl + mess_api
mess_resp = requests.get(url=mess_url, headers=headers)
mess_content = mess_resp.json()['text'] #This is not going to work, it is not a dictionary you can call a key on... yet
mess_room = mess_resp.json()['roomId'] #Same thing here as the variable above
return mess_room, mess_content #In python you can return multiple return items in a function and unpack to multiple variables
def send_message(room, text):
send_api = "/messages"
send_url = baseurl + send_api
send_data = {"roomId": room,
"text": text
}
send_resp = requests.post(send_url,
headers=headers,
json=send_data)
return send_resp
def get_membership(room, email):
get_mem_api = "/memberships"
get_mem_param = {"roomId": room,
"personEmail": email # make sure the correct variale is referenced in this function
}
get_mem_url = baseurl+get_mem_api #some thing is missing...
get_mem_resp = requests.get(url=get_mem_url,
params=get_mem_param,
headers=headers)
print(get_mem_resp)
# This conditional flow "if something is True... do something... else, do something different"
# helps us control what happend next in our app / script.
# "or" statements say only one of my true false definitions need to be true.
#"and" would tell us both statements need to be true
if get_mem_resp.status_code != 200 or json.loads(get_mem_resp.text)["items"] == []:
get_mem_id = None
print("Did not get the right response, please check to make sure the e-mail is correct")
else:
get_mem_id = json.loads(get_mem_resp.text)["items"][0]["id"]
return get_mem_id
def rock_ban(mem_id):
del_mem_api = "/memberships/%s" % mem_id
ban_url = baseurl + del_mem_api
ban_resp = requests.delete(url=ban_url, headers=headers) #Looks reasonable, but something is not quite right...
return ban_resp
@app.route('/ban/this/guy', methods=['POST'])
def ban_hook():
spark_hook = request.get_json(silent=True)
hook_data = spark_hook['data']
mess_room, mess_content = get_message(hook_data)
#The split function can split strings into variables
#See simple tutorial on this here http://www.pythonforbeginners.com/dictionary/python-split
#you can define what to split by inside the parentheses "some,text,is,neat".split(",")
#if you leave it blank, like we did, it will split by a space.
mess_list = mess_content.split()
print(mess_list)
#This conditional is looking for specific elements in the message
# that was sent to the your bot.
# If the message doesn't meet these criteria it will move to the else condition.
# notice the statement mess_list[1] == "/ban": this is the main logic that we need to ban someone
if hook_data['personEmail'] != "2manybots@sparkbot.io":
if len(mess_list) >= 3 and mess_list[1] == "/ban" :
mem_id = get_membership(mess_room, mess_list[2])
print(mem_id)
if mem_id is None:
print("E-mail address not valid, user is not in this room!!!")
print("We didn't end up banning anyone, DOH!")
send = send_message(mess_room,
"I can exile anyone, just say the word. But they have to actually be in a room, and I have to be the moderator :-)")
print(send)
else:
banned_for_life = rock_ban(mem_id)
if banned_for_life.status_code == 204:
print("We banned that jerk %s" % mess_list[2])
else:
print("I don't think we banned them, well this is embarrassing...")
send = send_message(mess_room, "%s is now banned!!!!" % mess_list[2])
print(send)
else:
print("We didn't end up banning anyone, DOH!")
send = send_message(mess_room,
"I can exile anyone, just say the word. But they have to actually be in a room, and I have to be the moderator :-)")
print(send)
return json.dumps({"did-it-work": "A-OK"})
if __name__ == '__main__':
app.run(host="localhost", port=8080)