Update 'chatbot/actions/actions.py'
This commit is contained in:
+6
-137
@@ -1,14 +1,4 @@
|
|||||||
# This files contains your custom actions which can be used to run
|
|
||||||
# custom Python code.
|
|
||||||
#
|
|
||||||
# See this guide on how to implement these action:
|
|
||||||
# https://rasa.com/docs/rasa/custom-actions
|
|
||||||
|
|
||||||
|
|
||||||
# This is a simple example for a custom action which utters "Hello World!"
|
|
||||||
|
|
||||||
from typing import Any, Text, Dict, List
|
from typing import Any, Text, Dict, List
|
||||||
|
|
||||||
from rasa_sdk import Action, Tracker
|
from rasa_sdk import Action, Tracker
|
||||||
from rasa_sdk.executor import CollectingDispatcher
|
from rasa_sdk.executor import CollectingDispatcher
|
||||||
from flask import request
|
from flask import request
|
||||||
@@ -24,147 +14,26 @@ class ActionService(Action):
|
|||||||
tracker: Tracker,
|
tracker: Tracker,
|
||||||
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
||||||
buttons=[
|
buttons=[
|
||||||
{"payload":'/docs{"content_type":"blogs"}',"title":"yes"},
|
{"payload":'/satisfied',"title":"yes"},
|
||||||
{"payload":'/video{"content_type":"video"}',"title":"no"},
|
{"payload":'/notsatisfied',"title":"no"},
|
||||||
|
|
||||||
]
|
]
|
||||||
dispatcher.utter_message(text="Are you satisfied with above solution",buttons=buttons)
|
dispatcher.utter_message(text="Are you satisfied with above solution",buttons=buttons)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
class action_support_service(Action):
|
||||||
|
|
||||||
|
|
||||||
class ActionService(Action):
|
|
||||||
|
|
||||||
def name(self) -> Text:
|
def name(self) -> Text:
|
||||||
return "action_suport_service"
|
return "action_support_service"
|
||||||
|
|
||||||
def run(self, dispatcher: CollectingDispatcher,
|
def run(self, dispatcher: CollectingDispatcher,
|
||||||
tracker: Tracker,
|
tracker: Tracker,
|
||||||
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
||||||
buttons=[
|
buttons=[
|
||||||
{"payload":'/connect_support{"content_type":"text"}',"title":"yes"},
|
{"payload":'/connect_support',"title":"yes"},
|
||||||
{"payload":'/noconnect_support{"content_type":"text"}',"title":"no"},
|
{"payload":'/noconnect_support',"title":"no"},
|
||||||
|
|
||||||
]
|
]
|
||||||
dispatcher.utter_message(text="do you want me to connect to support team",buttons=buttons)
|
dispatcher.utter_message(text="do you want me to connect to support team",buttons=buttons)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
question=[]
|
|
||||||
answer=[]
|
|
||||||
class ActionSaveConversation(Action):
|
|
||||||
def name(self) -> Text:
|
|
||||||
return "action_save_conersation"
|
|
||||||
|
|
||||||
def run(self,
|
|
||||||
dispatcher: CollectingDispatcher,
|
|
||||||
tracker: Tracker,
|
|
||||||
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
|
||||||
|
|
||||||
|
|
||||||
sender_id = tracker.sender_id
|
|
||||||
#print(tracker.events)
|
|
||||||
conversation=tracker.events
|
|
||||||
import os
|
|
||||||
if not os. path.isfile('chats.txt'):
|
|
||||||
with open('chats.txt ','w') as file:
|
|
||||||
file.write("user_input $$$$ bot_reply\n")
|
|
||||||
chat_data=''
|
|
||||||
for i in conversation:
|
|
||||||
if i['event'] == 'user':
|
|
||||||
chat_data+=i['parse_data']['intent']['name']
|
|
||||||
print('user: {}'.format(i['text']))
|
|
||||||
a='user{}: {}'.format(sender_id,i['text'])
|
|
||||||
question.append(a)
|
|
||||||
if len(i['parse_data']['entities']) > 0:
|
|
||||||
chat_data+=i['parse_data']['entities'][0]['entity']+', '+i['parse_data']['entities' ][0]['value']+','
|
|
||||||
print('extra data:', i['parse_data']['entities'][0]['entity'], '=',
|
|
||||||
i['parse_data']['entities'][0]['value'])
|
|
||||||
elif i['event'] == 'bot':
|
|
||||||
print('{}'.format(i['text']))
|
|
||||||
b='Bot{}: {}'.format(sender_id,i['text'])
|
|
||||||
question.append(b)
|
|
||||||
# try:
|
|
||||||
# chat_data+=i['metadata']['utter_action']+','+i['text']+'\n'
|
|
||||||
# except KeyError:
|
|
||||||
# pass
|
|
||||||
chat_data+=i['text']+'\n'
|
|
||||||
|
|
||||||
else:
|
|
||||||
with open('chats.txt','a') as file:
|
|
||||||
file.write(chat_data)
|
|
||||||
#print(question)
|
|
||||||
import pandas as pd
|
|
||||||
df=pd.DataFrame(question)
|
|
||||||
print(df)
|
|
||||||
conversation=None
|
|
||||||
question.clear()
|
|
||||||
print(conversation)
|
|
||||||
print(question)
|
|
||||||
|
|
||||||
print(sender_id)
|
|
||||||
|
|
||||||
# print(answer)
|
|
||||||
# a=question
|
|
||||||
# b=answer
|
|
||||||
# import numpy as np
|
|
||||||
# import pandas as pd
|
|
||||||
# if len(a) > len(b):
|
|
||||||
# b = np.pad(b, (0, len(a) - len(b)), 'constant')
|
|
||||||
# else:
|
|
||||||
# a = np.pad(a, (0, len(b) - len(a)), 'constant')
|
|
||||||
# df=pd.DataFrame({'question':a,"answer":b})
|
|
||||||
# print(df)
|
|
||||||
dispatcher.utter_message(text="All Chats saved")
|
|
||||||
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ActionSaveConversation(Action):
|
|
||||||
def name(self) -> Text:
|
|
||||||
return "action_save_conersation"
|
|
||||||
|
|
||||||
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
|
||||||
# Get sender_id
|
|
||||||
sender_id = tracker.sender_id
|
|
||||||
|
|
||||||
conversation = tracker.events
|
|
||||||
chat_data = []
|
|
||||||
for i in conversation:
|
|
||||||
if i['event'] == 'user':
|
|
||||||
text = i['text'].replace('/connect_support{"content_type":"text"}', 'yes').replace('/noconnect_support{"content_type":"text"}','No').replace('/docs{"content_type":"blogs"}','yes').replace('/video{"content_type":"video"}','No')
|
|
||||||
chat_data.append({'sender': f'user_{sender_id}', 'message': text})
|
|
||||||
print(f'user_{sender_id}: {i["text"]}')
|
|
||||||
if len(i['parse_data']['entities']) > 0:
|
|
||||||
print('extra data:', i['parse_data']['entities'][0]['entity'], '=',
|
|
||||||
i['parse_data']['entities'][0]['value'])
|
|
||||||
elif i['event'] == 'bot':
|
|
||||||
print(f'bot_{sender_id}: {i["text"]}')
|
|
||||||
chat_data.append({'sender': f'bot_{sender_id}', 'message': i['text']})
|
|
||||||
|
|
||||||
with open('chats.json', 'w') as file:
|
|
||||||
json.dump({'conversation': chat_data, 'sender_id': sender_id}, file)
|
|
||||||
file.write('\n')
|
|
||||||
file.close()
|
|
||||||
print(json.dumps({'conversation': chat_data, 'sender_id': sender_id}, indent=4))
|
|
||||||
conversation = None
|
|
||||||
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
class ActionClearConversation(Action):
|
|
||||||
def name(self) -> Text:
|
|
||||||
return "action_clear_conversation"
|
|
||||||
|
|
||||||
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
|
|
||||||
|
|
||||||
sender_id = tracker.sender_id
|
|
||||||
tracker.events = []
|
|
||||||
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user