在这个版本中,我们启用了其他python包的预安装
python-webex-bot的Python项目详细描述
python_webex_bot
python3库旨在帮助您创建一个cisco webex teams bot,并利用这些bot可用的一些功能。 大多数为webex设置的python库都缺乏将您连接到webhook的能力,这是为了解决这个问题
安装和设置
以下是本文档假定您已经安装的项目
- virtualenv
- Python3
- ngrok
步骤1:设置虚拟环境
要初始化虚拟环境,请在命令行或命令提示符下运行以下命令
virtualenv venv
然后我们激活它:
窗口
venv\Scripts\activate
Linux
source venv/bin/activate
在那里,您可以设置虚拟环境并准备好执行操作
步骤2:安装python-webex-bot
在激活的虚拟环境中,运行以下命令通过pip安装python_webex_bot:
pip install python_webex_bot
然后下载将在并发步骤中使用的ngrok
快速启动
让我们在本地机器上安装一个简单的机器人,运行并响应。
步骤1:在Cisco Webex上创建机器人
如果你还没有,create your Webex account. 然后前往create your bot
应为您提供机器人程序的访问令牌。
获取此访问令牌并将其作为auth_令牌放入环境变量中。
这可以通过命令提示符或命令行完成:
set auth_token=my_auth_token
用您的bots访问令牌替换my_auth_令牌
这是运行bot的关键部分,因为python_webex_bot库使用它来标识您的bot
如果您还对环境变量、为什么需要它们以及如何使用它们有一些疑问,this可能是一个好的开始
步骤2:设置ngrok
在与步骤1和2中使用的终端不同的终端中,导航到放置ngrok的文件夹。
然后运行以下命令:
ngrok http 5000
这将产生与下面所示类似的输出:
Session Status online
Session Expires 7 hours, 59 minutes
Update update available (version 2.3.25, Ctrl-U to update)
Version 2.3.18
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://87a942a1.ngrok.io -> http://localhost:5000
Forwarding https://87a942a1.ngrok.io -> http://localhost:5000
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
现在您可以开始任务了
步骤3:创建python文件并运行它
创建一个运行bot的python文件。在我的例子中,我将命名我的文件run.py
复制并粘贴以下代码:
from python_webex.v1.Bot import Bot
from python_webex import webhook
bot = Bot() # the program will automatically know the bot being referred to y the auth_token
# create a webhook to expose it to the internet
# rememer that url we got from step 2, this is where we use it. In my case it was http://87a942a1.ngrok.io.
# We will be creating a webhook that will be listening when messages are sent
bot.create_webhook(
name="quickstart_webhook", target_url="http://87a942a1.ngrok.io", resource="messages", event="created"
)
# we create a function that responds when someone says hi
# the room_id will automatically be filled with the webhook. Do not forget it
@bot.on_hears("hi)
def greet_back(room_id=None):
return bot.send_message(room_id=room_id, text="Hi, how are you doing?")
# We create a default response in case anyone types anything else that we have not set a response for
# this is done using * [ don't ask me what happend when someone sends '*' as the message, that's on my TODO]
@bot.on_hears("*")
def default_response(room_id=None):
return bot.send_message(room_id=room_id, text="Sorry, could not understand that")
# make the webhook know the bot to be listening for, and we are done
webhook.bot = bot
if __name__ == "__main__":
webhook.app.run(debug=True) # don't keep debug=True in production
现在,当我们给机器人发短信“嗨”时,它会回答“嗨,你好吗?”
当我们发短信的时候,比如“我们什么时候能见面?”它会以“对不起,我不明白”来回应。
稍后将设置更多文档