Custom integration help
# 🤝help
g
Hey @crooked-van-25152 @acceptable-kangaroo-64719 I am trying to send an event to the botpress client via a custom integration, just like how Send Custom Event works but I am getting the error below, any reason what could cause it:
Copy code
Invoke Error     {"message":"Request failed with status code 401","name":"AxiosError","config":{"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"env":{},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json","x-bp-messaging-client-id":"0e46922c-1f42-43cc-8bb0-cad4a3d3d4cc","x-bp-messaging-client-token":"0e46922c-1f42-43cc-8bb0-cad4a3d3d4cc","User-Agent":"axios/0.27.2","Content-Length":270},"baseURL":"https://messaging.botpress.cloud/api/v1","method":"post","url":"/messages","data":"{\"conversationId\":\"d42854a6-c222-4871-928b-590aecb14521\",\"authorId\":\"0e46922c-1f42-43cc-8bb0-cad4a3d3d4cc\",\"payload\":{\"type\":\"trigger\",\"trigger\":{\"type\":\"custom-event\",\"event\":{\"link\":\"https://studio.botpress.cloud/0e46922c-1f42-43cc-8bb0-cad4a3d3d4cc/flows/wf-main\"}}}}"},"code":"ERR_BAD_REQUEST","status":401}
a
401 error is typically from the request not being formatted correctly. What code are you using to send the request?
g
Using a messaging client to send to send a message to the browser from the server
my code looks like this @acceptable-kangaroo-64719
Copy code
import type { IntegrationProps } from '.botpress'
import { MessagingClient } from '@botpress/messaging-client'



export const sendEvent: IntegrationProps['actions']['sendEvent'] = async (props) => {
    return sendTrigger(props)({
        type: 'custom-event',
        event: JSON.parse(`{"link":"${props.input.event}"}`),
    })
}

// Invoke Error     {"id":"err_1693445645238x4ECD7A08","code":400,"type":"ReferenceNotFound","message":"State \"webchat\" of type \"integration\" isn't defined for the integration \"fc99948c-ba97-4f1c-a1ce-ea0c0708afa9\""}

const sendTrigger = ({ client, input, ctx }) => async (trigger: any) => {
    const { conversation } = await client.getConversation({ id: input.conversationId })

    await send({
        message: {
            type: 'trigger',
            trigger,
        },
        ctx: ctx,
        conversation:conversation,
        client:client,
    })
    return {}
}

const send = async (params: { message: any, ctx: any, conversation: any, client: any }) => {
    const { message, ctx, client, conversation } = params
    const { configuration, integrationId } = ctx
    const { messagingUrl, botIda, clientToken } = configuration
    const botId = "0e46922c-1f42-43cc-8bb0-cad4a3d3d4cc"
    // const messagingClient = await getMessagingClient(ctx, client)
    const messagingClient = new MessagingClient({
        url: "https://messaging.botpress.cloud",
        clientId: "0e46922c-1f42-43cc-8bb0-cad4a3d3d4cc",
        clientToken: "0e46922c-1f42-43cc-8bb0-cad4a3d3d4cc",
      //   webhookToken,
      })
    
    const conversationId = getTag(conversation.tags, 'id')
    
    if (!conversationId) {
        throw new Error('No conversation id')
    }
    console.log(botId);
    await messagingClient.createMessage(conversationId, botId, message)
}

const getTag = (tags: Record<string, string>, name: string) => {
    return tags[`webchat:${name}`]
  }
a
@colossal-egg-20510 this looks properly formatted to me, is there aything you know about that could be causing a 401 error from his code?
g
Would appreciate your help @colossal-egg-20510
Could it be something in the definition? @acceptable-kangaroo-64719
a
@colossal-egg-20510 can triggers even be sent over the client API or is it limited to text payloads only?
c
@acceptable-kangaroo-64719 Yes the trigger can be sent this way but using the
messagingClient
to send a message to a conversation that was created by the webchat integration will be hard. There is a
webhookToken
necessary to do those calls which is created by the webchat integration and isn't available publicly.
@gentle-shampoo-88367 As we are approaching the release of the new chat api to manage conversation through api we'll release a documentation that will allow you to make those kind of custom conversation management. But, for now, this is a really though feature to implement on your side. I highly suggest waiting a little while for the new api to be ready.
g
@colossal-egg-20510 thanks for getting back to me
I ppreciate the detailed response. Can you guide me how to get the webhook token? An overview would be fine as well. I have a deadline for this integration by then of this week. And the core depends on sending an event to the user's browser
Would highly appreciate any guidance
c
Sure, but first have you tried using the
Send Custom Event
action of the webchat integration ? It looks like it is pretty similar to what you're trying to achieve.
@gentle-shampoo-88367 If the
Send Custom Event
doesn't fit your needs I can do a one time exception to provide you the Webhook token of your bot. You can DM me the bot Id.
g
Perfect @colossal-egg-20510
Just DM'd @colossal-egg-20510
@colossal-egg-20510 if you could respond 🙂
c
He seems to be ooo, he should reply to you tomorrow am 🙂
g
ahh dang 😓
c
@gentle-shampoo-88367 I am back sorry something's came up. I'm on it.
g
No worries mate. You are a saviour @colossal-egg-20510
12 Views