A Cisco Webex bot is an automated user within the Webex platform that can be interacted with, enhancing the user experience within an organisation.
This first task will get you all set up to run a very simple hello bot that replies with a basic message.
Go to Cisco Webex for Developers and click Sign up on the top right corner. Fill in your details and follow the instructions to create an account.
Go back to Cisco Webex for Developers and log in with your account details.
Click Documentation on the top bar and select the Bots section on the left. On this Bots Documentation webpage you will find an extended explanation on what Bots are and how to create them.
To proceed, click on the Create a Bot button and fill up all the required information to describe your new Bot. Finally, scroll down and click on the Add Bot button.
Now that your Bot has been created, save the Access Token since you will need it later.
"Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency." Link to install git.
"Python is an interpreted, high-level, general-purpose programming language" Link to install the latest python version.
"Ngrok exposes local servers behind NATs and firewalls to the public internet over secure tunnels." Link to download ngrok.
Open a terminal and you can start working on your home directory (/Users/<username> for macOS, <root>\Users\<username> for Windows or /home/<username> for Linux). Otherwise, you can navigate to another directory using cd <other-directory>.
Clone the git repository to your local machine by running the following command on your terminal.
git clone git@github.com:tomtvr/webex-python-bot-project.git --recursiveTo go to the directory you have just cloned simply run cd webex-python-bot-project. Try running ls and check that you can see all the files you will need to get your Bot up and running.
After installing Python, open terminal and run the following command to install dependencies.
pip3 install flask requests ./webexsdkUnzip the ngrok file that you downloaded above and copy the executable file to the webex-python-bot-project folder. On a terminal window, go to this directory and run the following command to expose a web server on port 12000 of your local machine to the internet.
./ngrok http 12000Open task1.py with your favourite text editor. If you still do not have one, take a quick look at Visual Studio Code, Atom or (in case you are a very brave developer) Vim.
Replace <my-bot-access-token> on Line 8 with the Access Token you saved during the Create a Bot step. Remember to copy the token into the string provided, otherwise it won't be recognised correctly.
On the terminal window, run the following to get your bot working.
python3 task1.pyLogin to your Webex account and Create a Space by clicking the + button. Then, enter your Bot Username (something like XXXX@webex.bot).
To start interacting with the bot, type @<bot_name> along with your message. For example, if the bot was called "HelloBot" you would type @HelloBot hello
The goal of this task is for you to get the poll bot up and running and make a few test polls with the rest of your team.
Follow the steps of the previous task to create a new bot called "Poll Bot". Grab and insert the access token into the string replacing <bot-access-token> with your token value.
To run the bot, execute in a terminal the command:
python3 task2.pyMake sure that ngrok is also running like in the previous task in a separate terminal.
Login to your Webex account and Create a Space by clicking the + button. Then, enter your Bot Username (something like XXXX@webex.bot) along with all the people you want to take part in the poll. The bot has four commands: create poll, add option, start poll and end poll. To invoke one of those commands, type @<bot_name>, a space, and then the command. For example @PollBot create poll.
- Start by using the
create pollcommand to create an initial poll. The bot will message you 1 to 1 with a card to get started. - Everyone in the space can then add options for the poll using the command
add option. An adaptive card will be messaged 1 to 1 to add the option to the poll. - Once all the options have been added use the command
start poll. An adaptive card should now appear in the space for people to start voting. - Once everyone has voted run
end pollto get the results in the space.
For this section, the aim will be to extend the functionality of the poll bot. As you will have seen from experimenting with it in the previous section, it is quite limited in terms of what it can do. You will be adding new features of your own and addressing some issues with the current implementation.
The first new feature that we will add is a help command that gives the user a list of the available commands along with a brief description of what each command does. After all, the bot is pretty useless if people don't know how to interact with it!
-
Exercise: Using the code in
task3.py, add a new commandhelpthat posts a help message either directly to the user or in the space. This can be just in plain text.Hint
You may find it helpful to use the functions
send_direct_messageorsend_message_in_roomwhich have already been defined for you.
It would also be nice to be able to check the status of an ongoing poll, so that the author can preview the results before closing the poll.
-
Exercise: Define a new command
status pollthat shows the title and description of the poll, whether the poll has started, and if available, the preliminary results.This should also include number of votes for each option, as well as the total number of votes submitted.
As a developer, you need to be thinking about what can go wrong in your program! In this section, we will identify and address potential sources of errors.
Discuss in your group what would happen if:
- You enter a command that isn't recognised.
- When creating a poll, you don't enter a title and/or description before clicking submit.
- You don't create a poll before trying to start one.
- You create a poll but don't add any options before trying to start it.
- You try to add an option after a poll has already started.
- No votes are recorded before the poll is closed.
- You try to create a second poll
- Exercise: Try these out to detemine what the current behaviour is and have a think about what the desired behaviour would be.
In many of the cases in the previous exercise, the program just fails silently :( When things go wrong we should be letting the user know!
- Exercise: Upon receiving an unknown command, send an error message in the space and suggest trying
helpto view list of available commands.
When writing error messages, it is best practice to be clear, precise, and contructive. Note how in the message above, a solution is provided.
- Exercise: If the user tries to start a poll without creating one first, send an error message in the space and suggest trying
create poll.
In general, users should receive feedback when they invoke a command or submit a reponse to confirm the success of the action. In this task, when a user submits a vote we would like to send them a confirmation message.
- Exercise: Upon submitting a vote, send the user a direct message to confirm that their vote was recorded successfully. This should include the user's choice.
Currently, once invoking the create poll command and submitting the description for a poll, any subsequent attempts to use create poll are ignored. This is something we need to change!
-
Exercise: After a poll has ended, allow the user to create a new one. Make sure that the options and votes recorded are reset.
-
Exercise: If a user tries to create a new poll while there is a poll in progress, send a message to tell the current poll needs to be closed before you can create a new one.
As things are, we've got an issue! People can vote multiple times, can you think of a good way to prevent this?
- Exercise: Only allow one vote per person. Either send a direct message to the user that they cannot vote more than once, or let the user change their vote and send a new confirmation message.
For this challenge, we will be adding a new commmand ping poll that sends a direct message to every member of the space that hasn't submitted their vote yet.
- Exercise: Implement
ping pollthat fulfills the description above.
When all the members of the space have submitted their votes, it would be nice to have the poll end automatically instead of waiting for the author to end it manually.
- Exercise: When everyone has already voted, automatically end the poll and display results.
Debugging code is a necessary part of programming. Sometimes stepping though the code seeing how values change can help you track down tricky bugs!
Insert the following at the location you want to break into the debugger:
import pdb; pdb.set_trace()When the line above is executed, you will be dropped into the interactive debugger. For an introduction to the various debugger commands you can use, have a look at the link provided in the resources section.
VSCode has a package for running and debugging python executables. Make sure to install the VSCode python extension: https://marketplace.visualstudio.com/items?itemName=ms-python.python
To run the task files in VSCode click on the "Run and Debug" section and hit the play button. Breakpoints can be added to the necessary lines and you can interatively step through the debugger.
More information can be found here: https://code.visualstudio.com/docs/python/debugging
Now it's time to take everything you have learnt from the previous few tasks and create a bot of your own. 🥳
The theme of this bot should be focussed around improving productivity.
Here are a few ideas to get you started:
- A bot to help remind you of important events coming up
- A bot to help make notes while in class or a meeting
These are just a few ideas, to help with inspiration check out the bot page here: https://apphub.webex.com/bots
- Cisco Webex for Developers: Platform documentation
- Webex Teams APIs: Webex Teams SDK documentation
- Webex Cards Guide: Webex Teams SDK documentation for sending Adaptive Cards
- Adaptive Cards Spec: Schema Explorer for Adaptive Cards and interactive online demo
- Cisco Webex Teams App Hub: Get some inspiration to develop your own bot from this list of Cisco Webex Teams Bot examples
- RapidAPI: The world's largest API directory
- Python Debugger: An interactive source code debugger for Python programs