A cookiecutter template to easily create deployable Webex Teams bots in python.
Features include
- Fully customizable setup of webhooks from a configuration file using webhooksimple
- Comes with support for adaptive cards in native python (using pyadaptivecards)
- Comes with
Dockerfile
anddocker-compose.yml
for container deployment - flask-based bot served by gunicorn
Create a new project based on this cookiecutter by issuing the following command
$ cookiecutter https://github.com/sQu4rks/cookiecutter-pywebex-teams-bot
In order to develop your bot you will want to make it able to receive webhooks. To do so you need a way of receiving http requests i.e. ngrok or serveo. You will also need a bot access token that you can obtain from developer.webex.com.
To setup your local machine for development just run
$ source developer_setup.sh
This script will ask you for your bot access token and your remote prefix i.e. https://d342d393.ngrok.io
(no trailing slash).
The template code comes prepared with functions to handle all webhooks available within the Webex Teams API and you can automate the setup of the webhooks using webhooksimple. To setup all webhooks simply run
$ python3 -m webhooksimple sync
inside of your project directory.
Your bot code is ready to be modified in bot.py
. Happy coding!
Once you have finished your bot you can create a docker image using the deployment_setup.sh
script.
This script will
- Log you into docker hub
- Create a new build of your bot image
- Push the newly created image to docker hub.
When deploying the container make sure to set the WEBEX_TEAMS_ACCESS_TOKEN
and REMOTE_PREFIX
environment variables. The entrypoint.sh
script used by the container upon start will setup the webhooks according to these environment variables.
You can customize the webhooks you want to use. In order to keep your deployment and development environment the same you should not edit the vars.yml
file that is created during the developer setup but rather edit the vars.yml.conftpl
file that will be used during deployment on your remote host.
- Delete the
vars.yml
file (i.e.rm vars.yml
) - Edit the
vars.yml.conftpl
file. In the webhooks section at the bottom of the file you can find the list of resources (i.e. messages, rooms, memberships) and events (i.e. created, deleted). Delete (or comment out) all events and or resources you don't need. - Run config templater to create a new
vars.yml
file by issuing thepython3 -m config_templater
command. - Run
python3 -m webhooksimple setup
to delete all webhooks currently registered for your bot and overwrite them with the newly specified webhooks.
In the bot directory is a file called template.py
that can be used to create new function stubs for each of the webhooks specified in vars.yml
. Simply run the script and copy&paste the resulting code into the bot.py
file.