def POST(self):
"""Respond to inbound webhook JSON HTTP POSTs from Cisco Spark."""
json_data = web.data() # Get the POST data sent from Spark
print("\nWEBHOOK POST RECEIVED:")
print(json_data, "\n")
webhook_obj = Webhook(json_data) # Create a Webhook object from the JSON data
room = api.rooms.get(webhook_obj.data.roomId) # Get the room details
message = api.messages.get(webhook_obj.data.id) # Get the message details
person = api.people.get(message.personId) # Get the sender's details
print("NEW MESSAGE IN ROOM '{}'".format(room.title))
print("FROM '{}'".format(person.displayName))
print("MESSAGE '{}'\n".format(message.text))
# This is a VERY IMPORTANT loop prevention control step.
# If you respond to all messages... You will respond to the messages
# that the bot posts and thereby create a loop condition.
me = api.people.me()
if message.personId == me.id:
# Message was sent by me (bot); do not respond.
return 'OK'
else:
# Message was sent by someone else; parse message and respond.
if "/CAT" in message.text:
print("FOUND '/CAT'")
cat_fact = get_catfact() # Get a cat fact
print("SENDING CAT FACT '{}'".format(cat_fact))
response_message = api.messages.create(room.id, text=cat_fact) # Post the fact to the room where the request was received
return 'OK'
评论列表
文章目录