Hi all, I tried to save rasa chatbot conversation in csv file but it’s not working.Anybody knows how to save rasa chatbot conversation please let me know
Hi, you can use tracker.event in action.py file.
Hi @sonam thank you so much for your response. I tried this thing but I’m able to create csv file with headings like user inputs,bot replies etc but unable to save whole conversations. please go through this code once you are free
class ActionSaveConversation(Action):
def name(self) -> Text:
return "action_save_conversation"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
conversation=tracker.events
import os
if not os.path.isfile('chat.csv'):
with open('chat.csv', 'w') as file:
file.write("intent,user_input,entity_name,entity_value,action,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']))
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('Bot: {}'.format(i['text']))
try:
chat_data+=i['metadata']['utter_action']+','+i['text']+'\n'
except KeyError:
pass
else:
with open('chat.csv','a') as file:
file.write(chat_data)
dispatcher.utter_message(text="All chats are saved sucessfully!!!")
return []
Doing this with a custom action isn’t feasible since you’d have to include that action in every rule & story.
I would configure the event broker and write a program that reads the tracker events and write it to your csv.
Hi,
Okay there is a default way of storing conversation in database using Inmemory tracker store, you can read about it here,
Thanks