https://discord.gg/botpress logo
Anyone know if its possible for a user
# 🌎general
s
Anyone know if its possible for a user to drag a file into the chat and send it? I want to create some cards for the hub to push the files to Knowledge Base using the client so anyone can use it easily 🙂
w
i think it's not possible without writing some js directly in the web page where the bot is embedded..
s
Thinking the same but was hoping that wasn't the case! I I could possibly write that and share the code to do it but definitely would be a little harder for users to implement
w
sure..
s
Looks like @quick-musician-29561 had us covered almost a year ago: https://discord.com/channels/1108396290624213082/1189632494241321180/1189644402151391343 Probably a good way to combine this with the new API though
w
i made something to be able to upload something directly through the webchat (without adding a form in the page) maybe it could be better, but that can be a good start too i think
by sending event from the bot to the page
s
That would be amazing! Could I look at your example?
w
ah no, if I remember correctly it was by a more tricky means, but I thought it would have been even easier to use a sendCustomEvent from the webchat ! i made a video about it , and i will give the code

https://www.youtube.com/watch?v=bJsQv920GoM▾

Copy code
window.botpressWebChat.onEvent(
            (event) => {
                if (event.type === 'MESSAGE.RECEIVED') {
                console.log('A new message was received!')
                    if(event.value.payload.text === "Choisissez un fichier texte"){
                        const fileInput = document.createElement('input');
                        fileInput.type = 'file';
                        fileInput.addEventListener('change', (event) => {
                            const file = event.target.files[0];
                            console.log('Selected file:', file);
                            const reader = new FileReader();
                            reader.onload = async function(event) {
                                const fileContent = event.target.result;
                                const title = file.name;
                                console.log('File content:', fileContent);
                                console.log('File name:', title);
                                window.botpressWebChat.sendPayload({
                                    type: 'trigger',
                                    payload: {
                                        title: title,
                                        content: fileContent
                                    }
                                })
                            }
                            if(file.type.startsWith('image/')){
                                reader.readAsDataURL(file);
                            }
                            else{
                                reader.readAsText(file);
                            }
                            
                        });
                        fileInput.click();
                    }
                }
            },
            ['MESSAGE.RECEIVED']
        )
basically this is the js code (that can be modified with the better solution i just talked about - sendcustomevent)
s
Thank you @wooden-beard-40210 ! I'll see what I can whip up with all these examples ❤️
w
Can’t wait to see what you will build 😁
I was just thinking (idk if that could work) why not build an integration, so the code will be completely handled by it , and the builders will not have to paste some code in the webpage
So it will be easier for users to implement
I think there is a file reader npm package
2 Views