Webchat send proactive message

Hi there,

I included Botpress in my website using channel-web. I would like to get the bot to start the conversation using proactive trigger, so I included the following code into the folder “before_incoming_middleware”:

if (event.type === ‘proactive-trigger’) {
const payloads = await bp.cms.renderElement(‘builtin_text’, { text: ‘Hey there!’, typing: true }, event.channel)
bp.events.replyToEvent(event, payloads)

But I keep getting the following error message: “Syntax error: await is only valid in async function”

Could anyone help me with this?


Hey @alinewaeber !

The Syntax error: await is only valid in async function error message happens because your function is missing the async keyword. See https://stackoverflow.com/a/49432604 for a better explanation.

To fix this, simply add the async keyword before your hook function definition.
Your .js file in the before_incoming_middleware folder should look like this:

async function action(bp: typeof sdk, event: sdk.IO.IncomingEvent) {
  // make sure to prefix the hook() function with the async keyword
  async function hook() {
    if (event.type === 'proactive-trigger') {
      // ... your code goes here

  return hook()

Hope this helps!

1 Like