项目作者: Paul-weqe

项目描述 :
A python3 pip library meant to help you create a cisco webex teams bot.
高级语言: Python
项目地址: git://github.com/Paul-weqe/python_webex_bot.git
创建时间: 2018-07-18T18:10:00Z
项目社区:https://github.com/Paul-weqe/python_webex_bot

开源协议:MIT License

下载


published
PyPI version

Official Documentation

Installation and Quickstart

Rooms

Messages

Cards

python_webex_bot

A python3 library meant to help you create a cisco webex teams bot and take advantage of some of the features available to these bots.
Most of the python libraries setup for webex have been lacking in terms of connecting you to a webhook and this aims at solving that

Installation and setup

The following are items this documentation assumes you already have installed

Step 1: setup the virtual environment

to initialize the virtual environment, run the following command in your Command Line or Command Prompt

  1. virtualenv venv

then we activate it:

Windows

  1. venv\Scripts\activate

Linux

  1. source venv/bin/activate

and there, you have your virtual environment setup and ready for action

Step 2: install python_webex_bot

while still in your activated virtual environment, run the following command to install python_webex_bot via pip:

  1. pip install python_webex_bot

then download ngrok which will be used in the concurrent steps

Quickstart

Lets get a simple bot up, running and responsive on our local machine.

Step 1: Create the bot on Cisco Webex

If you haven’t already, create your Webex account.
Then head on to create your bot

You should be provided with an access token for the bot.

Take this access token and place it in your environment variable as auth_token.

this can be done via your Command prompt or Command Line as:

  1. set auth_token=my_auth_token

replace my_auth_token with your bots access token

This is a crutial part of running your bot as the python_webex_bot library uses this to identify your bot

If you still have some questions on environment variables, why we need them and how to use them, this may be a good start

Step 2: setup ngrok

in a different terminal from the one used in steps 1 and 2, navigate to the folder where you have the ngrok placed.

Then run the following command:

  1. ngrok http 5000

This should produce an output similar to the one shown below:

  1. Session Status online
  2. Session Expires 7 hours, 59 minutes
  3. Update update available (version 2.3.25, Ctrl-U to update)
  4. Version 2.3.18
  5. Region United States (us)
  6. Web Interface http://127.0.0.1:4040
  7. Forwarding http://87a942a1.ngrok.io -> http://localhost:5000
  8. Forwarding https://87a942a1.ngrok.io -> http://localhost:5000
  9. Connections ttl opn rt1 rt5 p50 p90
  10. 0 0 0.00 0.00 0.00 0.00

Now you are ready for the quest

Step 3: create the python file and run it

Create a python file where you intend to run the bot. In my case, I will name my file run.py

copy and paste the following code:

  1. from python_webex.v1.Bot import Bot
  2. from python_webex import webhook
  3. bot = Bot() # the program will automatically know the bot being referred to y the auth_token
  4. # create a webhook to expose it to the internet
  5. # rememer that url we got from step 2, this is where we use it. In my case it was http://87a942a1.ngrok.io.
  6. # We will be creating a webhook that will be listening when messages are sent
  7. bot.create_webhook(
  8. name="quickstart_webhook", target_url="http://87a942a1.ngrok.io", resource="messages", event="created"
  9. )
  10. # we create a function that responds when someone says hi
  11. # the room_id will automatically be filled with the webhook. Do not forget it
  12. @bot.on_hears("hi")
  13. def greet_back(room_id=None):
  14. return bot.send_message(room_id=room_id, text="Hi, how are you doing?")
  15. # We create a default response in case anyone types anything else that we have not set a response for
  16. # this is done using * [ don't ask me what happend when someone sends '*' as the message, that's on my TODO]
  17. @bot.on_hears("*")
  18. def default_response(room_id=None):
  19. return bot.send_message(room_id=room_id, text="Sorry, could not understand that")
  20. # make the webhook know the bot to be listening for, and we are done
  21. webhook.bot = bot
  22. if __name__ == "__main__":
  23. webhook.app.run(debug=True) # don't keep debug=True in production

Now, when we text our bot “hi”, it will respond with “Hi, how are you doing?”

And when we text anything else, like “When can we meet up?” it will respond with “Sorry, I could not understand that”

FIND MORE DOCUMENTATION here