簡單示例
一個對話簡單示例,詳見官方開發機器人應用。
建立 Feishu Bot 應用
在左側導航欄,進入【憑證和基礎資訊】,得到
App ID
和
App Secret
。
啟用機器人功能:在 【應用功能】中【機器人】設定 【啟用機器人】為開啟狀态。
進入【事件訂閱】,獲得 Verification Token。
建立 Chatopera Bot 應用
登入 Chatopera 雲服務
https://bot.chatopera.com
,建立【機器人】。
進入機器人設定頁面,得到
Client Id
Secret
以上提到了兩個“Bot 應用”,實際上是一個對話機器人應用的兩個部分:"Feishu Bot 應用" 是管道,"Chatopera Bot 應用"是自然語言對話管理;前者是“嘴”,後者是“腦”。
容器化部署對話機器人服務
編輯配置檔案
擷取源碼chatopera.feishu
# 建立 .env
# vim /data/chatopera/feishu/app/.env
export APP_ID=cli_xxx
export APP_SECRET=
export APP_VERIFICATION_TOKEN=
export CHATOPERA_CLIENT_ID=
export CHATOPERA_SECRET=
參數對應清單:
key | value | 描述 |
---|---|---|
APP_ID | App ID | 憑證和基礎資訊中擷取 |
APP_SECRET | App Secret | |
APP_VERIFICATION_TOKEN | Verification Token | 事件訂閱中擷取 |
CHATOPERA_CLIENT_ID | Client Id | Chatopera 聊天機器人設定頁 |
CHATOPERA_SECRET | Secret |
示例如下:
打包鏡像
# cd feishu/app
# cat requirements.txt
chatopera>=2.0.0
如果你不是容器化部署,采用本地部署,可能會遇到如下錯誤:
# 安裝 Python 依賴
pip install -r requirements.txt
# 如出現報錯:ModuleNotFoundError: No module named 'distutils.cmd'
sudo apt-get install python3-distutils
# 再次pip安裝即可
pip install -r requirements.txt
# 修改Dockerfile檔案
FROM chatopera/python37:latest
LABEL maintainer="[email protected]"
COPY ./app /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 8991
CMD ["/bin/bash", "-c", "./serve.sh"]
# build image
cd /data/chatopera/feishu
docker build -t harbor.test.com/chatopera/feishu-v1.0:20211109 .
# push image
docker push harbor.test.com/chatopera/feishu-v1.0:20211109
啟動服務
# run 容器
docker run -itd --name feishu-chatopera -p 8991:8991 harbor.test.com/chatopera/feishu-v1.0:20211109
# 檢視log
# docker logs feishu-chatopera
run app with CHATOPERA_BOT_PROVIDER https://bot.chatopera.com, CHATOPERA_CLIENT_ID xxxxxxxxxxxxxxx, APP_ID xxxxxxxxxxxxxxx
bot info: {'rc': 0, 'data': {'chatbotID': 'xxxxxxxxxxxxxxx', 'name': 'AI語音對話', 'primaryLanguage': 'zh_CN', 'description': 'AI語音助手對話機器人', 'welcome': '你好!我是AI語音助手機器人星期五', 'gambitLikeThreshold': 0.8, 'conversationTimeout': 1800, 'historyCheckpoints': 100, 'gambitIntentQuestionMaxAttempts': 2, 'fallback': '我不明白您的意思。', 'trans_zhCN_ZhTw2ZhCn': True, 'status': {'reindex': 0, 'reparse': 0, 'retrain': 0}}}
server is started, listen on port 8991 ...
服務預設使用
8991
端口,可以在 .env 中增加環境變量
PORT=YOUR_PORT
自定義。
部署 ngrok 服務
詳見The use of the internal network penetration artifact ngrok
配置
HTTPs
服務,接入飛書要求使用
https server URL
,測試目的建議使用
ngrok
# 使用 screen 背景運作
# 建立一個名為ngrok的會話視窗
screen -S ngrok
# 執行
ngrok http 8991
# 暫離視窗
Ctrl+a d(即按住Ctrl,依次再按a,d)
# 檢視存在的會話視窗
screen -ls
# 進入視窗
screen -r ngrok
screen -r 程序ID
# 關閉視窗
exit
screen使用,如上,得到 https URL 位址:" https://xxx.ngrok.io "。
配置飛書消息事件訂閱
再次進入 Feishu 【事件訂閱】 頁面,編輯【請求網址 URL】的值。将剛剛獲得的
https://xxx.ngrok.io
位址填寫上,儲存。
在飛書【事件訂閱】中添加事件--【接收資訊】,如下:
釋出上線機器人
應用設定權限
設定權限 進入飛書應用管理控制台,打開【權限管理】頁面,釋出新版本,并且選擇權限如下。
釋出到企業内部使用
進入飛書應用管理控制台,進入【應用釋出】打開【版本管理與釋出】頁面。
建立新版本,并送出,需企業内部管理者稽核方可通過。
添加對話機器人入群
建立好群組後,添加機器人: