Blog

webhookSimple - Easy-to-use Webhook Automation for Webex Teams

If you are developing anything with an API, there is one thing you have come across for sure: webhooks.

Webhooks are callbacks that you can specify inside an API. Every time a certain event (e.g. the creation of a message) occurs, a URL specified in the webhook will be called with additional information about the event (e.g. the message ID and the sender). This concept allows a developer to just react to a new message coming in instead of having to constantly poll the server for new messages and keep track of which messages the bot has already seen and which it hasn’t.

This concept is also implemented in Webex Teams, and it’s great. However, it can be a bit of a hassle to set up these webhooks, especially in a development environment where your public address might constantly change. This is where webhookSimple comes into play.

webhookSimple is a simple (and open source) Python framework/command line tool that allows you to quickly describe your desired Webex Teams webhooks and then creates or synchronizes them for you.

Let’s dive into it

webhookSimple requires two files from you: vars.yml and hooks.yml.

vars.yml specifies the different variables while hooks.yml lets you specify the webhooks themselves.

A webhook always looks like this (in hooks.yml):

---
hooks:
  - name: test hook 1
    resource: "messages"
    event: "created"
    target_url: "https://your_url_here"

Make sure that the name of your webhook is always unique since this is what webhookSimple will use to identify and synchronize your webhooks. Your vars.yml must include an adapter that specifies the kind of API we are interacting with as well as the authentication details. Leave this to the provided parser.WebexTeamsWebhookManager for now and add the access token in the correct spot.

vars.yml

adapter: parser.WebexTeamsWebhookManager
  - authentication:
		- access_token: add_your_access_token_here

You can now setup, purge, list, export or sync your webhooks.

  • setup will delete all webhooks currently present for this bot and create new ones based on the hooks.yml file.
  • sync will update all existing webhooks based on the name attribute and create those not present. It will not delete webhooks that are registered on the server.
  • purge will delete all webhooks without creating new ones
  • list will list all webhooks currently registered
  • export will save all your currently active webhooks to a .yml file

Invoke the module by running:

$ ls
hooks.yml vars.yml
$ python3 -m webhooksimple setup

Taking it one step further

Setting up webhooks from the command line based on a configuration file is already pretty cool and convenient. But what if we have ten webhooks and need to update the target_url on all of them? We’d have to manually edit all the webhook entries in hooks.yml. This is where the vars.yml file comes into play. hooks.yml is not a simple configuration file but rather a Jinja2 template of a configuration file. What you can do is this:

vars.yml

---
url_prefix: https://my_url_base

hooks.yml

---
access_token: add_webex_access_token_here
hooks:
  - name: test hook 1
    resource: "messages"
    event: "created"
    target_url: "https://{{ url_prefix }}/messages"

But this is not all. Those who have worked with Jinja2 before probably already know what is coming next. You can also add some (generator) logic here. Let’s say we want to create a debug and a production version of our webhook. We can do this by doing the following:

vars.yml

---
envs:
  - name: production
    url: https://my_production_prefix
  - name: development
    url: https://my_development_prefix

hooks.yml

---
hooks:
  {% for env in envs %}
  - name: {{ env.name }} message hook
    resource: "message"
    event: "created"
    target_url: {{ env.url }}/messages
  {% endfor %}

Or maybe you want to set up the same webhook for different URLs. This would look something like this:

vars.yml

---
urls:
 - https://url_number_1
 - https://url_number_2
 - https://url_number_3

hooks.yml

---
hooks:
  {% for url in urls %}
  - name: "hook for {{ url }}"
    resource: "message"
    event: "created"
    target_url: {{ url }}
  {% endfor %}

Happy programming! You can get webhookSimple by running

$ pip3 install webhooksimple

You can find the code here.

Next time: How to extend webhookSimple to include other formats than YAML

Some answers

You might have some questions. So let me provide some answers.

Why isn’t this called WebexTeamsWebhookSimple?

Well, first of all, that name is just too long, and second, this project is not necessarily restricted to the Webex Teams API. While the current implementation only provides an adapter to Webex Teams, it is easily extendable to other (Cisco) APIs. If you have a cool API to implement let me know on Twitter @squ4rks.

You want to do it yourself? Have a look into the webhooksimple.manager file. You need to specify a class that inherits from the abstract webhooksimple.manager.WebhookManager class and provides all the functionality listed there. Take a look at webhooksimple.manager.WebexTeamsWebhookManager for inspiration.

Why oh why did you use YAML?

Because I like it. I like it more than JSON/XML. But fear not: The parser is modular in the sense that you can easily create your own version that takes JSON or whatever you like. Have a look into the webhooksimple.parser module. Specifically, you need to extend webhooksimple.parser.Parser and implement the abilities the same way that webhooksimple.parser.YAMLParser does it for YAML.

Tags: python, Webex API

← Back to all posts