How to get website name in variable on which my chatbot is embedded

I have explored the docs of botpress and got stuck at a point. In my project I have to monitor that on which website my bot is running so I want to store website name at run time of bot in a variable and get it to me through mail. I found an environment variable BP_HOST, but don’t know how to use it in my flow.
Please help!! Urgent request @allardy @marcmerce

Hi, do you have access to the source code of the website? You could write simple javascript using Botpress SDK and send an event to Botpress with the URL of the website.

Here is the Botpress SDK

2 Likes

@marcmerce Yes, I can access to the source code of the website. Can you please explain how to send event to botpress with the URL ?

@shrianshjain

You can adapt the example found in our tutorials section : Proactive
Use the sendEvent method such as this one in your pages:

window.botpressWebChat.sendEvent({
type: ‘proactive-trigger’,
channel: ‘web’,
payload: {
text: ‘website URL’
}
})

then have a hook catch that event to send you that e-mail with the payload.text as the body of that e-mail.

Hopefully this will help you complete your project!

Happy Building :robot:

@Maxime_Joannette Thank you for your help !! :hugs:

@Maxime_Joannette @marcmerce event.payload.text is not working, it is catching the text which I just typed in my window.

//Script written in my source code of website
window.addEventListener(‘message’, function(event){
if(event.data.name === ‘webchatOpened’){
window.botpressWebChat.sendEvent({
type: ‘proactive-trigger’,
channel: ‘web’,
payload: { text: ‘cureway.in’}
})
}
})
//Script written in hooks/before_incoming_middleware/builtin/apply_nlu_contexts.js

if (event.type === ‘proactive-trigger’) {
const eventDestination = {
channel: event.channel,
target: event.target,
botId: event.botId,
payload: event.payload,
text: event.text,
location: event.location,
threadId: event.threadId
}

// Skip event processing
event.setFlag(bp.IO.WellKnownFlags.SKIP_DIALOG_ENGINE, true)

// Make the bot respond with custom content instead
bp.cms.renderElement(‘builtin_text’, { text: “”, typing: true }, eventDestination).then(payloads => {
bp.events.replyToEvent(event, payloads)
})
}

Please have a look and guide me right.

I’m not sure about a few things. Your page’s script is supposed to trigger as soon as you open the webchat, so before you even type in it you should already have received data, can you verify this?

Second, you noted that your hook

script [is] written in hooks/before_incoming_middleware/builtin/apply_nlu_contexts.js

Might I suggest you make a separate hook file that only processes this event?

Lastly, since the example suggests that the payload has a “fake message”, maybe the payload is discarded for certain events? I’ll let @allardy confirm if that is the case or not, he has better insight on what’s possible or not!

P.S. A comment on the last section of your code, since the webchat is just opened and you have all this event structure already, you should set up a welcome message instead of the empty string!

@Maxime_Joannette event.payload.text is not triggering any data, it is printing blank as I verified this by sending a custom text on proactive-trigger event, following the code:
bp.cms.renderElement(‘builtin_text’, { text: event.payload.text, typing: true }, eventDestination).then(payloads => {
bp.events.replyToEvent(event, payloads)
})
Second, Yes I verified the path of file.
If I make a separate hook file that only processes this event then how would I trigger that file from source code in my website ?
At last @allardy please help me in this issue.
I just wanna trigger my website name in the botpress in any variable and put that variable in my send mail module.
Also in docs I have found an environment variable BP_HOST, this basically return the hostname of website on which bot is running so if I will be able to use this one without any hard coding then it will be great, help me if you can tell me how to use it.
Thanks for your efforts guys :slightly_smiling_face:

Hooks are called automatically, in order of execution in the middleware chain, and in alphabetical order, see this diagram of the event engine: https://botpress.com/docs/main/overview#complete-overview-of-the-event-engine

This environment variable simply overrides the default httpServer.host property in Botpress configuration, it is the address at which you can connect to Botpress Admin, Studio or the chatbot itself. It has nothing to do with a site the bot is hosted on. Changing this property will only give you trouble if your current setup works.

With all of this, I still don’t have a better idea to help you out.

@Maxime_Joannette Thanks for your efforts!! @allardy Can you help in this ?

Hi @shrianshjain,

Did you try outputting your whole event in a console to troubleshoot? In your hook, try adding console.log(event) or bp.logger.info(event.payload) to inspect the whole object.

It seems to me the text you are sending in might be further in event.payload.payload.payload.text or something like that.