Skip to main content

Using python script, send a message to CISCO spark user when a certain event happens


CISCO spark has exposed few APIs that enable us to send a message to a certain email ID.
Another good part is that, instead of sending the message from a certain user, you can create a Spark Bot and then send a message to the required user from the bot's ID.

If you have a spark account, you can access the tutorials here and have fun.

Creating a bot instructions are available here:

I have tested these APIs and sent messages to myself from the bot's ID using Postman and a simple python script. You can download postman from the following link: https://www.getpostman.com/ and run the apis or run them directly from the interactive Rest API terminal that is present in one of the submenus in the above-mentioned link. You should try various other options as well. 

The Python script to send spark message:

import json
import requests

if __name__=="__main__":
    sparkUrl="https://api.ciscospark.com/v1/messages"
    data = {
        "toPersonEmail": "<username>@<domainname>",
        "text": "Hello World from my-bot python script. "
    }
    headers = {
        "Content-type": "application/json; charset=utf-8",
        "Authorization": "Bearer NDM0MmVjOTItZTM2MS00MmEyLWJiMmEtNDk0MTE1ZjE1NzQzMTE2NDVmZjYtZTdm"
    }
    r = requests.post(sparkUrl, data=json.dumps(data), headers=headers)

    print(r.content)
    print(r.status_code, r.reason)



Put the above block inside a method and call it when a certain event occurs and messages are sent. 
The next step would be on how to add the bot to a spark room and make it respond to messages that are sent mentioning the bot. This will require you run your bot interactive script to run on a server, create webhooks in spark room for the bot, and code responses that you intend to send in the script.

One example would be like sending a message in a room where the bot is added.
Let the bot name be my-new-bot
@my-new-bot /sayhelloworld
The response from the bot would be:
"Hi <username>@<domainname>, Hello World!!"

If possible, will write about the interactive bot implementation in the future. Until then, 
have fun. Happy coding! :) 


Comments

Popular posts from this blog

Concurrency in Go - Visualization

In this blog, let us discuss a program to visualize concurrency in Golang. Reference and source: Program 1: import " fmt " import " time " func main () { var Ball int Ball = 5 table := make ( chan int ) go player (table, 1 ) go player (table, 2 ) fmt. Println ( " mainstart: " , Ball) table <- Ball time. Sleep ( 1 * time. Second ) endres := <- table fmt. Println ( " mainend: " , endres) } func player ( table chan int , playernumber int ){ for { ball := <- table fmt. Println (playernumber, " : " ,ball) ball++ time. Sleep ( 100 * time. Millisecond ) table <- ball } } /* ---------------------------------------------------------------------------------------------------------- Explanation: Please try to visualize on your own before reading the explanation. We will name the two goroutines that are called as player1_goroutine ...

Implementing a file monitoring script using python.

The idea is to implement a method where a user will be able to modify an xml file and the modifications will trigger a certain action in the script. This has to be implemented in python. WatchDogs is a python library that helps in file system monitoring. API reference : https://pythonhosted.org/watchdog/quickstart.html#quickstart Useful link :  http://brunorocha.org/python/watching-a-directory-for-file-changes-with-python.html The example scripts provided in the above links give a good headstart to play around with the file system and figure what is the exact use case that you want to achieve. Current use case:  The python script that I run is bundled as a container. There are mounted volumes in the container. So using an external script I am planning to modify the xml file and since the same file is modified inside the container, the python thread that is listening to the file modification event will trigger an action like sending a mail, make an entry to ...

Cycling to Dommasandra - 25km circuit

The following blog post not only inspired me to go for more cycling trips and write regularly because of some valuable, loving comments from friends, it also motivated many of my friends to try it out. https://rkinsideout.blogspot.in/2017/10/the-one-where-raj-went-for-cyclingfirst.html I and my close friend Padma whom I have known from my school days decided to go for cycling. The following week was a Diwali week and most of our friends' had already started leaving to hometowns. We decided to do it anyway. I chose a circuit behind Kadubeesanahalli, Sarjapur as I knew it would be pleasant and also have seen people cycling in these places. The Circuit It was a pleasant ride as it was cloudy was most of the time. From Panathur to Sarjapur road, there were more vehicles on the road, but after Carmelaram, there were fewer people on the road. The road condition also was good till Dommasandra with few patches here and there. Since the road was good, the bigger vehicles were spe...