Apologies for the previous deleted comment, I was having problems with the forum.
The code I have is quite simple, the implementation of the Rasa Webchat:
A Custom Action called ActionLogout.
class ActionLogout(Action):
def name(self) -> Text:
return "action_logout"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
requests.get(domainUrl + "logout/", params={"csrfmiddlewaretoken":tracker.get_slot("csrfmiddlewaretoken")},
cookies={"sessionid":tracker.get_slot("sessionid"),"csrftoken":tracker.get_slot("csrftoken")})
if (tracker.get_slot("sessionid")):
print("Yes, there is a session started")
else:
print("No session started")
if (requests): # successful response
dispatcher.utter_message(text="The session has been successfully closed")
else:
dispatcher.utter_message(text="An error has occurred")
return []
I have been trying to make a connection with Sockets or with a Rasa Custom Channel, but I have not succeeded, since the Browser session is different and even if I want to send messages they are sent to another new session, so I can not connect to the Front end as I wish.
Anyway, the solution you propose is fully functional. It’s not an elegant way to solve the problem, but it’s the only option I have for now, thank you very much for your answer and excellent help!
In case anyone will find it useful in the future, I have implemented it as follows:
The Rasa Webchat widget has a parameter that allows you to listen to events from the Socket, although it only lets you know when a message has been sent and its text, it doesn’t inform you of the action it corresponds to or anything else… you can also know when it has connected and when it has disconnected from the Socket, but that’s not useful to me in this case.
- To add the parameter so that it calls a function when the bot sends a message:
...
initPayload: generateInitPayload(),
customData: { language: "en" },
socketUrl: "http://localhost:5005",
onSocketEvent: {
'bot_uttered': botMessageEvent,
'connect': function() {
console.log('connection established')
},
'disconnect': function() {
doSomeCleanup();
},
},
onWidgetEvent: {
onChatClose: function(widgetEvent) {
console.log('connection onChatClose')
},
onChatOpen: function(widgetEvent) {
console.log('connection onChatOpen')
},
onChatHidden: function(widgetEvent) {
console.log('connection onChatHidden')
},
},
// add other props here
},
...
In my case the event name is "bot_uttered"
, since that is the value I have set to the bot message event in the credentials.yml
file :
socketio:
user_message_evt: user_uttered
bot_message_evt: bot_uttered
session_persistence: false
- To read the event message and react accordingly:
function botMessageEvent(e) {
console.log('the bot said something: ' + e.text);
if (e.text === "The session has been successfully closed") {
// Do something really cool
}
}
If someone finds a more elegant way to know what actions are called, please comment, thanks!