botpress / v12

Botpress OSS – v12
https://v12.botpress.com
GNU Affero General Public License v3.0
68 stars 83 forks source link

Add custom key values in the window.botpressWebChat.init method #852

Closed Namec closed 4 years ago

Namec commented 4 years ago

Hello all,

I am new to botpress and trying To achieve a task that can make me send data through window.botpressWebChat.init method. And then storing this additional data as temp variable.

Is there any solution to fetch the url custom params

Best

allardy commented 4 years ago

Hi @Namec , what are you trying to achieve exactly? You can trigger an event sent by the user by sending window.botpressWebChat.sendEvent({type: 'my_custom_event', somedata: 'hello' }) , then capture the event on Botpress.

Namec commented 4 years ago

Hello @allardy yes this is exactly what i a m trying to achieve, but i need that process trigered at the begining of the chat session to route the flow the right was.

Last but not the least, how To get Back this data with my flow.

Best

allardy commented 4 years ago

@Namec Please have a look at the proactive example in the documentation: https://botpress.io/docs/tutorials/proactive#send-message-when-the-webchat-is-loaded

It triggers once the webchat is loaded, then you can send the event with your custom payload.

You can create a hook to fetch the data. Ex:

image

Namec commented 4 years ago

@allardy thank you for you help, this is exactly what i am looking for. you'r the best

and this can make me really add any process i want. i just need to master the syntax and now all the variables.

in my hook i am trying to store the value of event.payload.text, on a temp variable, and the get it back in the flow to show it to the user

Namec commented 4 years ago

i have succeded getting and parsing a custom payload each time webchatOpened ! and that is what i am looking for.

but now i 'am trying to store that parsed values, in temp variable, but i dosen't work !! or i don't to now how to write it down. (assigning a value to a temp variable and then getting that value back.

bt bug
allardy commented 4 years ago

@Namec When using the code editor, you have access to the typings, which auto-completes variables for you. There is a small difference between actions and hooks. In actions, we added a shortcut to access temp variables.

You can see the difference in signatures below:

Action

image

Hook

image

Basically, you just need to use event.state.temp instead of using directly temp when using the hook

Don't hesitate to move your mouse over squiggly lines, it will tell you why there's an error

image

BTW, if you skip the dialog engine, the event will not trigger any flow, and when it ends, the temp variable is cleared. You can set the flag FORCE_PERSIST_STATE, and I recommend you use either session or user instead of temp (user is kept indefinitely for the user, while session is kept until the session timeouts, by default 30 mins)

epaminond commented 4 years ago

@Namec , I assume you were able to succeed with your task so closing this for now.