cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
4172
Views
20
Helpful
3
Comments
Dirk-Jan Uittenbogaard
Cisco Employee
Cisco Employee

blog-botwebsockets-title.jpg TIME: 5 minutes (if pipenv is installed)   DIFFICULT? No TESTED ON: MacOS, Windows 10

WHY?

  • Really easy to quickly test a bot
  • Can be used if your bot code runs inside the firewall
    (Without publicly accessible URL for webhooks to talk to)
  • Easy to run your own bot demo
  • No server/hosting/platform dependencies
  • Don't want to use Websockets but Ngrok with Webhooks? Check out my easy "Bot with Webhooks" code!​​​​​​​

Instructions

A - Prepare

  • Create a Webex bot 
  • Download the code HERE  
  • Check if you have python: "python -V"
    version should be 3.9 or higher
  • Check if you have pip: "python -m pip -V"
    'pip' is used to install Python libraries

  ​​​​​B - Create a folder for the bot and copy the bot files to this folder

  1. "mkdir webex-bot-ws"
  2. "cd webex-bot-ws"
  3. copy bot files to this folder (webexwebsocket.py, webex-bot-ws.py)
    (When using Pipenv, also copy "Pipfile")

 

______________ C/D/E/F below: only if you use Pipenv ______________  

C - Install "Pipenv" (if not installed)

  1. Check if you have pipenv: "pipenv -V"
  2. If not, install it: "python -m pip install pipenv"

D - Create Pipfile: (or use the supplied Pipfile)

  1. (Mac) "touch Pipfile" (creating the file)
  2. Edit "Pipfile"
  3. Paste content below in the Pipfile and save it. The folder now contains 1 file: "Pipfile" and Pipenv is ready to do its job.

 

 

[source]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "webex-bot-websocket"
[dev-packages]
[packages]
uuid = "*"
webexteamssdk = "*"
websockets = "*"
[requires]
python_version = "3.9"

 

  E - Setup virtual environment

 

  1. "Pipenv install"
    Based on the Pipfile, this creates a local environment with the required packages.

  F - Activate the created virtual environment

  1. "Pipenv shell"
    Your prompt changes to indicate you are in the "isolated" setup.​​​​​​​

  ​​​​​​​NOTE: When you start the bot, you need to be in the Pipenv shell. Otherwise, it will not have the right libraries available. Alternatively: type "pipenv run python webex-bot-ws.py" This directly runs the python code inside the pipenv environment.  ______________ C/D/E/F above: only if you use Pipenv ______________

 

G - Set bot token environment variable

  • (MacOS): export MY_BOT_TOKEN='YOUR_TOKEN_HERE'
  • (Windows): set MY_BOT_TOKEN=YOUR_TOKEN_HERE

H - Run the Bot (Python code)

  1. "python webex-bot-ws.py"  
  2. When the "___start_____" message appears, test the bot!

I - Test the bot

  1. Send "test" to the bot.
  2. It should repeat your message.
  3. Send "hello" to the bot.
  4. It should respond differently.
    in the terminal/cmd window you can see the code showing its activity.

  NEXT? update the "process_message" function to make it respond to the commands that you are sending.




Pipenv!

WHAT DOES PIPENV DO? Create a Pipfile with your package & python requirements. Then run "pipenv install" to create an isolated environment with the python version and the required python libraries. These are not installed system-wide but inside the 'virtual environment' that you can (de)activate. GREAT? Yes, it is great to test a setup or library without having to install it system wide. You can have multiple virtual environments that each have different versions, libraries etc., without affecting the system wide setup. DIFFFICULT? No. Really, it isn't as scary as it sounds, just follow instructions!

WebSockets?

The WebSocket Protocol establishes full-duplex, bidirectional communication between a client and server. This two-way flow is unique to WebSocket connections, and it means they can transfer data very quickly and efficiently.
blog-botwebsockets-explain.jpg

   

 

LIKE?

In order for me to create presentations, demos, labs and more:  let me know you liked it  



FAQ

Q: Does this work inside the corporate Firewall?
A: Yes, it most likely does   
Q: Can these websockets wash my car?
A: Sure, but only when - in the evening - it's colder than outside.   

 

Do this without Pipenv?

Use the instructions above, except:

- skip step C, D, E and F
- install the required Python libraries manually:

  • python -m pip install requests
  • python -m pip install webexteamssdk
  • python -m pip install flask

- Continue with step G

 

MORE developer resources & projects:  cs.co/webexdevinfo  

3 Comments
Mohan Kantamsetti
Cisco Employee
Cisco Employee

Thank you so much, it's a great blog, I tried the one with ngrok and was able to use my webex bot. This article also points to ngrok code and not the one with websockets, but I was able to find it in the same github account.

Using ngrok isn't suggested ? As it open an tunnel to our device to the outside internet ?
Thank you once again for the code and the blog!

 

Dirk-Jan Uittenbogaard
Cisco Employee
Cisco Employee

@Mohan Kantamsetti  Thank you for the kind words and the feedback. I have updated the URL so it points to the right code!

robad
Level 1
Level 1

Thanks for this post !

I must say that some companies are blocking Ngrock App (I heard that Cisco also).

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: