The following can be prepared
Create a server that returns 200 OK in Python
webhookchatwork.py
import http.server
import socketserver
import json
import requests
class MyHandler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
self.send_response(200)
self.end_headers()
content_leng = int(self.headers.get("content-length"))
req_body = self.rfile.read(content_leng).decode("utf-8")
json_object = json.loads(req_body)
print(json_object)
#Set 3000 ports in your local environment
with socketserver.TCPServer(("", 3000), MyHandler) as httpd:
httpd.serve_forever()
webhookchatwork.py, execute it belowpython
ngrok http 3000
result:

So far, the local webhookchatwork.py file has been published to the internet.
When posting from ChatWork, you must set it to receive in webhookchatwork.py.
Enter the URL of ngrok in the red frame above
After filling in, I think you can work with Chatwork webhook and Ngrok.
Verification:
webhookchatwork.py:python
python3 webhookchatwork.py

Return to 200 OK again.

There are two ways to handle ChatBot
webhookchatwork.py
APIKEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX' #Obtained from Chatwork API token
ENDPOINT = 'https://api.chatwork.com/v2'
ROOMID = 'XXXXXXXXX' #The room you want to post
post_message_url = '{}/rooms/{}/messages'.format(ENDPOINT, ROOMID)
headers = { 'X-ChatWorkToken': APIKEY }
def checkAssignee(assigneenum): #Person to reply
if assigneenum ==3987766:
assigneename = 'Mr. A'
elif assigneenum ==4388605:
assigneename = 'Mr. B'
return assigneename
# #Send to chatwork- start
def sendtoChatworkRemind(answer,fromaccountid,sendtouser):
headers = { 'X-ChatWorkToken': APIKEY }
params = { 'body': '[To:'+str(fromaccountid)+']'+ sendtouser+ '\n' + str(answer)
}
requests.post(post_message_url,
headers=headers,
params=params)
#Send to chatwork- end
webhookchatwork.py
chatbot = ChatBot('EventosChatBot')
trainer = ListTrainer(chatbot)
traningjson = []
print(json_object['webhook_event']['body'].find("[To:2555387]Chat Bot Eventos"))
if "[To:2555387]Chat Bot Eventos" in json_object['webhook_event']['body'] :
fromaccountid = json_object['webhook_event']['from_account_id']
reponse = json_object['webhook_event']['body'].replace("[To:2555387]Chat Bot Eventos\n","")
sendtouser = checkAssignee(fromaccountid)
if "Let me tell you]:)" in json_object['webhook_event']['body'] :
for item in json_object['webhook_event']['body'].split("\n"):
if "Question:" in item:
print(item.strip().replace("● Question: ",""))
if item.strip().replace("● Question: ","") != "":
traningjson.append(item.strip().replace("● Question: ",""))
else:
traningjson.append("")
if "Answer:" in item:
print(item.strip().replace("● Answer: ",""))
if item.strip().replace("● Answer: ","") != "":
traningjson.append(item.strip().replace("● Answer: ",""))
else:
traningjson.append()
trainer.train(traningjson)
answer = "Thank you for teaching(bow)"
sendtoChatworkRemind(answer,fromaccountid,sendtouser)
else:
answer = chatbot.get_response(reponse)
sendtoChatworkRemind(answer,fromaccountid,sendtouser)
Create a Google extension so anyone can train on your chatbot
content_scropts.js
setTimeout(function(){
$("#__chatworkInputTools_toolbarIcons").prepend('<li class="_showDescription __chatworkInputTools_toolbarIcon" id="teachChatbot" role="button" aria-label="info: Surround selection with [chatbotEV] tag"><span class="btnDanger" style="padding: 3px 4px; font-size: 10px; border-radius: 3px; position: relative; top: -2px;">chatbotEV</span></li>');
$("#teachChatbot").click(function(){
$("#_chatText").val('[To:2555387]Chat Bot Eventos \n let me know]:) \n[info]\n ● Question: \n ● Answer: \n[/info]');
})
},1000);
result:

Training:
I'm studying properly

Reply:

Recommended Posts