https://discord.gg/botpress logo
BotPress with Clerk/NextJS
# 🤝help
n
Hello Everybody, My team has several legacy app using clerk/nextjs to ugrade with a chatbot. In most cases, the chatbot is going to be the main product. For us, it implies the usage should be restricted and tracked with Clerk. At the moment we are exploring possibilities, either using botpress or doing a custom bot with vercel AI SDK/openai api/Postgre/pg vector. we have two questions about Botpress: 1) how botpress is relevant for our use case ? we are not doing a support bot, more like a gpt wrapper with custom/inhouse data. 2) Is there a template somewhere with an integration of clerk with botpress? We looked at the doc but need a second opinion. We are looking for a clean long term solution. In particular we really need a proper way to control usage/access to the bot, not just a UI restriction.
g
depends what your use case is.... the second question is answerable, you build a web app that has access to your business logic, then you measure usage through your bot, it sends events to your web app. your webapp detecs this, and measures against your clerk metadata, and then decides whether or not to give access. to make it smooth i suggest sending an event to your bot when to restrict access and pivot the conversation off to a payment workflow to gently push the user to pay
n
For the first question: Ideally, the bot can retrieve data from a few pdf of 100-200 pages and query an external api endpoint which hide a postgre db. For the second question: does it imply to use the React Library, build a custom webchat (without using the Webchat component) with the useWebchat hook and listen to events (for instance the "message" event) with the final goal to update clerk metadata? Is it the best way to do it?
g
you dont need to use the react library: Send data from bot to website: https://botpress.com/docs/webchat/interact/send-custom-events/from-webchat#send-custom-events-from-webchat-to-your-website Send data from website to bot: https://botpress.com/docs/webchat/interact/send-custom-events/to-webchat you can use the standard webchat and vanilla js to do that. Regarding your use case, you want it to read PDFs, create some SQL and query it against a DB, its definitely possible, look into using knowledge base to store the pdfs (if you constantly rotate them, I would use the files api to programatically update/rotate the pdfs) and im pretty sure that botpress has a posgres integration, if that doesnt suit you though you can call it from your website.
ah one thing, and when you send the data to the website, you write the botpress event listener, then you send JSON string in the event field. you can distinguish between events of different types. for example (I dont know how you measure usage) but say you allow 10 queries per user per month or something. every message the user sends, send for example {"type": "message-sent"} then within your event listener,
Copy code
if (event.type === "message-sent") {
// update clerk publicMetadata with usage count
}
n
Yes, we will count user messages. Alright, thank you for messages, i'm going to look it today, and will provide an update.
So after a day: i was able to integrate the chatbot in a nextjs app: - when using the react library, it worked. - when using the embedded code, it didn't work. I moved forward using the react library, and i was not able to integrate event listener in the nextjs app window.botpress.on('message', (message) => { console.log('New message: ', message); // Insert your code here }); The issues I encountered: 1) the npm @botpress/webchat documentation and the "share" button code example were not up to date, not 100% sure, but they are for the 2.. version and the current version is 3.. 2) I didn't find a basic template with even listeners to copy. I didn't use the useWebchat hook, given that i would like to keep the builtin Webchat component. is there somewhere a template I can copy ? or do you know how to do it ? for now i'm not trying to listen to custom events, just the default ones. https://cdn.discordapp.com/attachments/1427196561250848881/1427596554264379483/image.png?ex=68ef7065&is=68ee1ee5&hm=c4183fa0d4b0e5a8e48a85ad83add705e7583057067024cbe85ebf55e5dedb68& https://cdn.discordapp.com/attachments/1427196561250848881/1427596554629546076/image.png?ex=68ef7066&is=68ee1ee6&hm=326e09ce0fd205a68e4be8c9ef0f4cc439e198051c0459aba0bb7596c461404f&
g
did you use this in your react app:
Copy code
window.botpress.on('message', (message) => {
    console.log('New message: ', message);
    // Insert your code here
});
if you did I believe that is the vanilla js one. If you're using the Webchat React library, you should use the
useWebchat
hook's returned
on
value to listen to events. You add this at the top level of your React component that renders Webchat.
Copy code
import { useWebchat } from '@botpress/webchat'; 
import { useEffect } from 'react'; 

function MyWebchatComponent() { 
  const { on } = useWebchat(); 

  useEffect(() => { 
    const unsubscribe = on('message', (event) => { 
      console.log('Message event received:', event); 
      // You can access message details from the event object 
      // For example: event.data.message.text 
    }); 

    return () => { 
      unsubscribe(); // Clean up the event listener when the component unmounts 
    }; 
  }, [on]); // Re-run effect if 'on' function changes 
}
I havent made a react based integration myself, so I may be wrong, but its worth a try. If so it may be a good idea to get into the office-hours live stream and directly ask there.