In this section, you will look at add some basic messaging functionality in the portal via the Bot you created earlier.
Step 1 - Add the Bot access token to the Project
With the SDK software already installed, implement a send_message() function
using the following steps:
-
First, you need to add your Bot's access token to the Portal code. As with other settings,
you will store this in the .env file and the code will read the values from
the environment variables that are set based on the contents of the file.
In the VS Code tab, open up the
.env file.
-
In the file, you will see where the WBX_BOT_ACCESS_TOKEN
variable is assigned. The value needs to be
set to your Bot's access token which should should be able to find in the
wbx_bot_access_token variable in the examples/wbx_messages.py file
you completed earlier in this lab.
-
Be sure that the token is surrounded by single quotes and that the
level of indentation remains the same.
- If you lost your Bot's access token, if you completed the examples, look at the
wbx_bot_access_token in examples/wbx_messages.py;
otherwise, you will need to follow these steps to generate a new one:
-
Access Webex developer portal
and log in with email:
pod6wbxuser@collab-api.com
and password:
C1sco.123
-
Click on your user button in the top right
then My Webex Apps
-
From there, click on your Bot and finally Regenerate Access Token. This will
invalidate the old token and create a new one.
-
Save this file
Step 2 - Add Message send capability
-
In VS Code, open up flaskr/api/v1/wbxt.py
-
Now that the access token variable is set, you can initialize the WebexSimpleApi
object that instantiates the wxc_sdk. The code issues a call to the
Get My Own Details
API and stores the result in the bot variable. The application needs this information to determine
if a message originated from the Bot itself later in the code.
Add the following to your code:
-
In this API, the room_name and text (the message to be sent)
will be populated via the args variable in the request. All you have to do is:
- Get a list of rooms
- Search the results for the room name
- Send a message to that room
This is the same as the earlier example, with the exception that you will search through all Rooms to find one
with a particular Room title, and some basic error and exception handling.
-
Save this file
-
In VS Code, start (or restart) your Flask app by clicking
Debug > Start Debugging (or
Restart Debugging, if already started).
The options for interacting with Webex are as extensive as the API itself. For
more information on the individual methods supported, take a look at the
user guide. Since it is a wrapper
for the API, it doesn't really work to extend capabilities present, it simply allows them
to be used from Python as simply as using the API documentation itself.
But first, you need to make sure this works by attempting to send a message.
Step 3 - Send a Message on Behalf of Bot
-
Make sure you are logged in to Webex. If need be, sign in with email address:
pod6wbxuser@collab-api.com
and password:
C1sco.123
-
Access the RESTPlus/Swagger UI page at http://dev1.pod6.col.lab:5000/api/v1/
-
Expand the wbxt section.
-
Click the POST /wbxt/send_message method.
-
Click Try it out.
-
For the room_name enter:
LTRCOL-2574 Space
This room_name must match the Room title of the Space you created earlier.
Additionally, the Bot must still be a member of
this Space. If the Bot was removed from the Space, you can re-add it by inviting the
participant:
cl2024-pod6-bot@webex.bot
-
In the text section, enter
Test Message
.
-
In the expanded section, click Execute.
-
Examine the response populated below. You should note a Server response code of
200 and a response body.
-
Verify the message was received in the Webex client.