Last active
February 21, 2016 17:42
-
-
Save TristanAmond/8384638d185bd6a64517 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_pushes(self, power_pin): | |
# Set variables | |
new_msgs = [] | |
website_bot_id = "REDACTED" | |
print(self.initial_pull) | |
# Send GET request to GroupMe | |
with requests.Session() as c: | |
# If this is the first cycle the system is running | |
if self.initial_pull == True: | |
payload = { "token" : "REDACTED", "limit": 1} | |
self.initial_pull = False | |
# If this is not the first cycle | |
else: | |
payload = { "token" : "REDACTED", "after_id": self.last_id} | |
# Receive response and load it from JSON into a List (new_msgs) | |
response = c.get("https://api.groupme.com/v3/groups/REDACTED/messages", params=payload) | |
response_dict = json.loads(response.text) | |
response_data = response_dict['response'] | |
# Create tuple from message id, sender id, and message content, then append to new_msgs | |
for item in response_data['messages']: | |
message = (str(item['id']), str(item['sender_id']), str(item['text'])) | |
new_msgs.append(message) | |
print(len(new_msgs), " new messages received.") | |
# If new messages have been received | |
if len(new_msgs) > 0: | |
for item in new_msgs: | |
# Message content is assigned a variable for ease of reading | |
curr_msg = item[2] | |
#If command is water | |
if curr_msg == ("water"): | |
print("Water command received.") | |
self.send_command_response("Water Command received") | |
self.run_pump(power_pin) | |
#If command is reboot | |
if curr_msg == ( "reboot"): | |
print("Reboot command received") | |
self.send_command_response("Reboot Command received") | |
self.send_notification("System set to reboot...") | |
os.system("sudo reboot now") | |
break | |
# set most recent message id to last_id variable for next cycle | |
self.last_id = new_msgs[len(new_msgs) - 1][0] | |
print("last_id set as ", self.last_id) | |
def send_command_response(self, content): | |
# bot_id connects with created bot on dev.groupme.com, text is message | |
payload = { | |
"bot_id" : "REDACTED", | |
"text" : content | |
} | |
with requests.Session() as c: | |
c.post("https://api.groupme.com/v3/bots/post", data=payload) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment