Basic example of a Webex bot that automatically manages it's own webhooks, making sure they point to the correct Ngrok public URL.
Time to setup: if you have setup a bot and have Python installed you can get this to work in 5-10 minutes.
A Prepare
python -V
The version should be 3.9 or higherpython -m pip -V
'pip' is used to install Python librariesB Create a folder** for the bot and copy the bot files to this folder
mkdir webex-bot-ngrok
cd webex-bot-ngrok
C Install "Ngrok"
ngrok
______________ BELOW: D/E/F/G only if you use Pipenv ______________
D Install "Pipenv" (if not installed)
pipenv -V
python -m pip install pipenv
E Create Pipfile (or use the provided Pipfile)
touch Pipfile
(or create a file called "Pipfile")url = "https://pypi.python.org/simple"
verify_ssl = true
name = "webex-bot-using-ngrok"
[dev-packages]
[packages]
requests = "*"
webexteamssdk = "*"
flask = "*"
[requires]
python_version = "3.9"
F Setup virtual environment
Pipenv install
G Activate the created virtual environment
Pipenv shell
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-ngrok.py
This directly runs the python code inside the pipenv environment.
______________ ABOVE: D/E/F/G only if you use Pipenv ______________
H Set bot token environment variable
export MY_BOT_TOKEN='YOUR_TOKEN_HERE'
set MY_BOT_TOKEN=YOUR_TOKEN_HERE
I Run the Bot (Python code)
python webex-bot-ngrok.py
___start_____
message appears, test the bot!J Test the bot
test
to the bot.hello
to the bot. The not should respond differently.webserver port (default: 4111) -- variable:
webserver_ port = 4111
enable debugging (default: False) -- With debugging enabled your code will be relaunched every time you save your python code. Variable:
webserver_debug = False
Ngrok status port number (default for the first Ngrok tunnel you open = 4040) -- Can be useful if you run multiple Ngrok tunnels they will all have the same status URL but with a different port number (4040, 4041, 4042 etc.) Variable:
ngrok_port = 4040
Bot token in code instead of an environment variable (bad practice but if you have your reasons, I won't stop you)
How? After "my_bot_token=", replace the os.getenv with your bot token between quotes and remove the if clause after this statement.
NGROK tunnel expires after 2 hours? Create a (free) Ngrok account and go here to get your API token and see how to use it. After that, no code modifications are needed.
I don't want to/can use Ngrok check out my alternative solution: Webex Bot using Websockets
Go to cs.co/webexdevinfo
Owner
Contributors
Categories
CollaborationProducts
WebexProgramming Languages
PythonLicense
Code Exchange Community
Get help, share code, and collaborate with other developers in the Code Exchange community.View Community