手把手教你創建聊天機器人來協助網路運營
作者:Melisha Dsouza
翻譯:吳金笛
校對:國相潔
本文約3000字,建議閱讀10 分鐘。
本文是創建聊天機器人實現網路運營的簡單教程。
在本教程中,我們將了解如何利用聊天機器人來協助網路運營。隨著我們向智能化運營邁進,另一個需要關注的領域是移動性。用一個腳本來進行配置,修復甚至故障檢修是很好的,但它仍然需要有人在場來監督,啟動甚至執行這些程序或腳本。
Nokia』s MIKA 是一個很好的聊天機器人例子,操作人員可以用它來進行網路故障排除和修復。根據 Nokia』s blog,MIKA會根據此單個網路的實際情況給出一個警報優先順序信息,並將當前的情況與該網路和其他網路過去事件的整個服務歷史進行比較,以確定當前問題的最佳解決方案。
Nokia』s MIKA
https://networks.nokia.com/services/digital-assistant-as-a-service
Nokia』s blog
https://www.nokia.com/blog/chatting-bots-home-work
讓我們創建一個聊天機器人來協助網路運營。對於這個用例,我們將使用一個被廣泛使用的聊天應用程序Slack。參考Splunk的智能數據分析能力,我們將看到一些用戶與聊天機器人的互動,從而對環境有一些了解。
本教程摘自AbhishekRatan撰寫的名為《Practical Network Automation – SecondEdition》的書。 這本書將向您介紹網路自動化的基本概念,並幫助您提高你的數據中心的穩健性和安全性。
可以在GitHub中找到本教程的代碼:
Github鏈接:
https://github.com/PacktPublishing/Practical-Network-Automation-Second-Edition/tree/master/Chapter0
在我們部署web框架時,我們將利用相同的框架與Slack聊天機器人進行交互,後者又將與Splunk進行交互。它還可以直接與網路設備交互,所以我們可以發起一些複雜的聊天,例如在需要時從Slack重啟路由器。這最終為工程師提供了移動性,工程師可以在任何地方(甚至是手機)處理任務,而不必被固定在某個地點或辦公室。
要創建聊天機器人,以下是基本步驟:
1.在Slack上創建一個工作區(或帳戶):
2.在你的工作區創建一個應用程序(在我們的例子中,我們創建了一個叫做mybot的應用程序):
3. 以下是關於該應用程序的基本信息(App ID 和Client ID可以與唯一標識此應用程序的其他信息一起使用):
4.為此應用程序添加bot功能:
5.添加事件訂閱並映射到將要發布消息的外部API。
事件訂閱是指某人在聊天中鍵入對聊天機器人的引用,然後使用該聊天機器人與聊天中被輸入的數據調用這個API:
這裡,一個關鍵的步驟是,一旦我們輸入了接受聊天消息的URL,就需要從Slack驗證這個特定的URL。驗證就是API端點以字元串或JSON的格式返回,與從Slack發送的響應相同的響應。如果我們收到相同的響應,Slack確認該端點是可信的並將其標記為已驗證。這是一個一次性的過程,並且API URL中的任何更改都將導致此步驟的重複。
以下是Ops API框架中響應特定查詢的Python代碼:
1.importfalcon
2.importjson
3.defon_get(self,req,resp):
4.# Handles GET request
5.resp.status=falcon.HTTP_200# Default status
6.resp.body=json.dumps({"Server is Up!"})
7.defon_post(self,req,resp):
8.# Handles POST Request
9.print("In post")
10.data=req.bounded_stream.read()
11.try:
12.# Authenticating end point to Slack
13.data=json.loads(data)["challenge"]
14.# Default status
15.resp.status=falcon.HTTP_200
16.# Send challenge string back as response
17.resp.body=data
18.except:
19.# URL already verified
20.resp.status=falcon.HTTP_200
21.resp.body=""
這將驗證,如果從Slack發出一個」challenge」,它將返回相同的」challenge」,這個值確認該端點是Slack頻道發送聊天數據的正確端點。
6.安裝此應用程序(或聊天機器人)到任何頻道(這類似於在群聊中添加用戶):
響應特定聊天信息的核心API框架代碼執行以下操作:
確認任何發送到Slack的信息在三秒內響應200次。如果沒有這樣,Slack報告:endpoint not reachable.
確保任何從聊天機器人(不是來自任何真實用戶)發出的信息不作為回複信息再次被發回。這可能會造成循環,因為從聊天機器人發送的消息將被視為Slack聊天中的新消息,然後它將被再次發送到URL。這最終會使聊天無法使用,從而導致聊天中出現重複的消息。
使用將被發送回Slack的令牌對響應進行身份驗證,以確保來自Slack的響應來自經過身份驗證的源。
代碼如下:
1.importfalcon
2.importjson
3.importrequests
4.importbase64
5.fromsplunkqueryimportrun
6.fromsplunk_alexaimportalexa
7.fromchannelimportchannel_connect,set_data
8.classBot_BECJ82A3V():
9.defon_get(self,req,resp):
10.# Handles GET request
11.resp.status=falcon.HTTP_200# Default status
12.resp.body=json.dumps({"Server is Up!"})
13.defon_post(self,req,resp):
14.# Handles POST Request
15.print("In post")
16.data=req.bounded_stream.read()
17.try:
18.bot_id=json.loads(data)["event"]["bot_id"]
19.ifbot_id=="BECJ82A3V":
20.print("Ignore message from same bot")
21.resp.status=falcon.HTTP_200
22.resp.body=""
23.return
24.except:
25.print("Life goes on. . .")
26.try:
27.# Authenticating end point to Slack
28.data=json.loads(data)["challenge"]
29.# Default status
30.resp.status=falcon.HTTP_200
31.# Send challenge string back as response
32.resp.body=data
33.except:
34.# URL already verified
35.resp.status=falcon.HTTP_200
36.resp.body=""
37.print(data)
38.data=json.loads(data)
39.#Get the channel and data information
40.channel=data["event"]["channel"]
41.text=data["event"]["text"]
42.# Authenticate Agent to access Slack endpoint
43.token="xoxp-xxxxxx"
44.# Set parameters
45.print(type(data))
46.print(text)
47.set_data(channel,token,resp)
48.# Process request and connect to slack channel
49.channel_connect(text)
50.return
51.# falcon.API instance , callable from gunicorn
52.app= falcon.API()
53.# instantiate helloWorld class
54.Bot3V=Bot_BECJ82A3V()
55.# map URL to helloWorld class
56.app.add_route("/slack",Bot3V)
執行頻道交互響應
此代碼解釋聊天頻道中使用chat-bot執行的特定聊天。此外,這將使用對特定用戶或頻道ID的響應,以及對Slack API 的身份驗證令牌進行響應。
Slack API:
https://slack.com/api/chat.postMessag
這保證Slack聊天的消息或回復顯示在其起源的特定頻道上。作為示例,我們將使用聊天對特定值進行加密或解密。
例如,如果我們寫入encrypt username[:]password,它將返回一個利用base64值加密的字元串。
類似地,如果我們寫入decrypt, 聊天機器人會返回一個加密字元串解密後的字元串。
代碼如下:
1.importjson
2.importrequests
3.importbase64
4.fromsplunk_alexaimportalexa
5.channl=""
6.token=""
7.resp=""
8.defset_data(Channel,Token,Response):
9.globalchannl,token,resp
10.channl=Channel
11.token=Token
12.resp=Response
13.defsend_data(text):
14.globalchannl,token,res
15.print(channl)
16.resp = requests.post("https://slack.com/api/chat.postMessage",data="{"channel":"" channl "","text":"" text ""}",headers={"Content-type":"application/json","Authorization":"Bearer " token},verify=False)
17.
18.defchannel_connect(text):
19.globalchannl,token,resp
20.try:
21.print(text)
22.arg=text.split(" ")
23.print(str(arg))
24.path=arg[0].lower()
25.print(pathin["decode","encode"])
26.ifpathin["decode","encode"]:
27.print("deecode api")
28.else:
29.result=alexa(arg,resp)
30.text=""
31.try:
32.foriinresult:
33.print(i)
34.print(str(i.values()))
35.forjini.values():
36.print(j)
37.text=text " " j
38.#print(j)
39.iftext==""ortext==None:
40.text="None"
41.send_data(text)
42.return
43.except:
44.text="None"
45.send_data(text)
46.return
47.decode=arg[1]
48.except:
49.print("Please enter a string to decode")
50.text=" argument cannot be empty"
51.send_data(text)
52.return
53.deencode(arg,text)
54.
55.defdeencode(arg,text):
56.globalchannl,token,resp
57.decode=arg[1]
58.ifarg[1]=="--help":
59.#print("Sinput")
60.text="encode/decode "
61.send_data(text)
62.return
63.ifarg[0].lower()=="encode":
64.encoded=base64.b64encode(str.encode(decode))
65.if"[:]"indecode:
66.text="Encoded string: " encoded.decode("utf-8")
67.send_data(text)
68.return
69.else:
70.text="sample string format username[:]password"
71.send_data(text)
72.return
73.try:
74.creds=base64.b64decode(decode)
75.creds=creds.decode("utf-8")
76.except:
77.print("problem while decoding String")
78.text="Error decoding the string. Check your encoded string."
79.send_data(text)
80.return
81.if"[:]"instr(creds):
82.print("[:] substring exists in the decoded base64 credentials")
83.# split based on the first match of "[:]"
84.credentials = str(creds).split("[:]",1)
85.username = str(credentials[0])
86.password = str(credentials[1])
87.status ="success"
88.else:
89.text="encoded string is not in standard format, use username[:]password"
90.send_data(text)
91.print("the encoded base64 is not in standard format username[:]password")
92.username ="Invalid"
93.password ="Invalid"
94.status ="failed"
95.temp_dict = {}
96.temp_dict["output"] = {"username":username,"password":password}
97.temp_dict["status"] = status
98.temp_dict["identifier"] = ""
99.temp_dict["type"] = ""
100.#result.append(temp_dict)
101.print(temp_dict)
102.text=" " username " " password
103.send_data(text)
104.print(resp.text)
105.print(resp.status_code)
106.return
此代碼查詢Splunk實例以查找與聊天機器人的特定聊天。聊天會請求當前關閉的任何一個管理介面(Loopback45)。 此外,在聊天中,用戶可以查詢所有具有up管理介面的路由器。 此英語響應將被轉換為Splunk查詢,並根據Splunk的響應將狀態返回到Slack聊天。
讓我們看看對Slack聊天執行操作來響應結果的代碼:
1.fromsplunkqueryimportrun
2.defalexa(data,resp):
3.try:
4.string=data.split(" ")
5.except:
6.string=data
7.search=" ".join(string[0:-1])
8.param=string[-1]
9.print("param" param)
10.match_dict=
11.fornoinrange(2):
12.print(match_dict[no].split(" "))
13.print(search.split(" "))
14.test=list(map(lambdax:xinsearch.split(" "),match_dict[no].split(" ")))
15.print(test)
16.print(no)
17.ifFalseintest:
18.pass
19.else:
20.ifnoin[0,1]:
21.ifparam.lower()=="up":
22.query="search index="main" earliest=0 | dedup interface_name,router_name | where interface_name="Loopback45" and interface_status="up" | table router_name"
23.elifparam.lower()=="down":
24.query="search index="main" earliest=0 | dedup interface_name,router_name | where interface_name="Loopback45" and interface_status!="up" | table router_name"
25.else:
26.return"None"
27.result=run(query,resp)
28.returnresult
下面的Splunk查詢來獲取狀態:
對於UP介面——「查詢」如下:
index="main"earliest=0 | dedup interface_name,router_name | whereinterface_name="Loopback45" and interface_status="up" |table router_name
對於DOWN介面(除此以外的任何狀態)——「查詢」如下:
index="main"earliest=0 | dedup interface_name,router_name | whereinterface_name="Loopback45" and interface_status!="up" |table router_name
讓我們看看與聊天機器人聊天的最後結果以及根據聊天記錄發回的響應。
編碼/解碼示例如下:
正如我們所見,我們發送了一個帶有encode abhishek[:]password123 信息的聊天。此聊天作為POST請求發送到API,後者又將其加密到base64並添加到Encoded string: 之後被返回。在下一個聊天中,我們使用decode選項傳入相同的字元串。這將通過解碼來自API函數的信息進行響應,並且用用戶名abhishek和密碼password123來響應Slack聊天。
讓我們看一看Splunk查詢聊天的示例:
在此查詢中,我們關閉了rtr1上的Loopback45介面。通過Python腳本,在我們對這些介面預定的發現過程中,數據位於Splunk中。當查詢到哪個管理介面(Loopback45)關閉時,它將用rtr1響應。Slack聊天將「On which routers the management interface is down」傳遞給API,在收到有效載荷時,後者將運行Splunk查詢以獲取統計信息。返回值(在本例中為rtr1)將作為聊天中的響應信息被返回。
類似地,「On which routers the management interface is down」的反向查詢將查詢Splunk並最終共享rtr2, rtr3, 和rtr4的響應(因為所有這些路由器上的介面都是UP)。
此聊天用例可以被擴展,以確保使用簡單聊天來進行完整的端到端故障排除。使用各種後端功能可以構建從問題的基本識別到複雜任務的大量案例,例如基於已識別情況的修復。
總結
在本教程中,我們實現了一些真實的用例,並研究了使用chatbot執行故障排除的技術。這些用例使我們深入了解了執行智能修復以及進行大規模審查,這是當前環境中的關鍵挑戰。
要學習如何利用Python的強大功能,毫無困難地自動化你的網路,請查看我們的書:《Practical Network Automation – Second Edition》。
原文標題:
Creating a chatbot to assist in network operations [Tutorial]
https://hub.packtpub.com/creating-a-chatbot-to-assist-in-network-operations-tutorial/
譯者簡介
吳金笛,雪城大學計算機科學碩士一年級在讀。迎難而上是我最舒服的狀態,動心忍性,曾益我所不能。我的目標是做個早睡早起的Cool Girl。
TAG:數據派THU |