https://discord.gg/botpress logo
#🤝help
i cannot connect my bot to my MySQL database server running on my local machine
# 🤝help
h

helpful-oil-7096

02/08/2024, 3:56 AM
I am new at development Hai everyone, i cannot connect my bot to my MySQL database server running on my local machine. I am using an execute code after the capture information for it insert the variable content that my user inputs in my local database. I want to capture the name,email and the feedback of my users
f

future-psychiatrist-80851

02/08/2024, 4:00 AM
hey @helpful-oil-7096 Please utilize your server's public URL in Botpress to populate data in the database.
h

helpful-oil-7096

02/08/2024, 4:01 AM
Good day, you mean i'll create an API endpoint?
f

future-psychiatrist-80851

02/08/2024, 4:05 AM
yes
h

helpful-oil-7096

02/08/2024, 4:10 AM
Sorry, I am new to web development. Can you please clarify how to do it? I still cannot understand the terms.
f

future-psychiatrist-80851

02/08/2024, 4:18 AM
can you explain how to use database in botpress?
h

helpful-oil-7096

02/08/2024, 4:22 AM
i will be using the botpress to capture the feedback of my users. I am operating a college website native php i already included the botpress and it works fine. But my insturctor suggested to include a feedback mechanism in my chatbot. First there will be an option saying Do you want to leave a feedback if yes my bot will first capture the name of the user second the email and third the feedback Then after it those 3 must be stored in my mysqldatabase
f

future-psychiatrist-80851

02/08/2024, 4:45 AM
okay
Use the Flask API on your local computer to try inserting data into the database. Then, call this API in Botpress and attempt to populate data.
h

helpful-oil-7096

02/08/2024, 4:46 AM
Thank you for helping me out
f

future-psychiatrist-80851

02/08/2024, 4:47 AM
In which technologies do you work?
h

helpful-oil-7096

02/08/2024, 4:48 AM
I do not have any, i'm at college as a graduating student
l

limited-pencil-78283

02/08/2024, 6:13 PM
Create a python flask API to accept data from Botpress and update MySQL as mentioned by @future-psychiatrist-80851 . To access this endpoint from Botpress it needs to be accessible over the internet.
h

helpful-oil-7096

02/13/2024, 3:47 AM
Update about this python flask API i create a file named app.py and also got some codes to it i use the postman to try it and it work, it inserted the data in my local database and my problem right now is how to configure the botpress
this is mycode in botpress but still receiving errors const request = require('request') const bp = require('botpress') // Initialize Botpress const server = bp.createBotpress() server.start() // Function to submit feedback to Flask API function submitFeedbackToAPI(user) { const feedbackData = { user_name: user.first_name, // Assuming first_name is available in user data user_email: user.email, // Assuming email is available in user data user_feedback: 'Your feedback goes here' // You'll need to retrieve user feedback } const options = { uri: 'http://localhost:5000/api/submit_feedback', method: 'POST', json: feedbackData } request(options, (error, response, body) => { if (!error && response.statusCode === 200) { console.log('Feedback submitted successfully') } else { console.error('Error submitting feedback:', error) } }) } // Example trigger point to collect feedback bp.hear(/give feedback/i, async (event, next) => { const user = event.user submitFeedbackToAPI(user) await event.reply('Thank you for your feedback!') next() })
f

fresh-fireman-491

02/13/2024, 4:13 PM
Which errors are you getting?
h

helpful-oil-7096

02/14/2024, 10:33 AM
Good evening for now there are no errors my code in the botpress is executed without error but my localsql database do not update it is like no data has been sent
I am using user variables in botpress
this is my execute code in botpress
my code in app.py
Good day @future-psychiatrist-80851 i have this http://127.0.0.1:5000/api/submit_feedback? and tried it in postman and i successfully inserted data in my database but right now i do not know how to do this to make it work in botpress utilize server's public URL
f

future-psychiatrist-80851

02/14/2024, 10:51 AM
Utilize ngrok to expose your local URL as a public URL.
h

helpful-oil-7096

02/14/2024, 10:53 AM
ok i'll do it
Hai I used the ngrok and it works fine in postman for testing
But in my botpress my code is executed but still it does not work no data is inserted in my database
f

fresh-fireman-491

02/15/2024, 3:10 PM
Can you try and publish your bot and try from the webchat?
h

helpful-oil-7096

02/15/2024, 3:25 PM
I have tried it but still it is not working
f

fresh-fireman-491

02/15/2024, 3:50 PM
And its still not giving any errors on either side?
Have you checked the logs for the published version
h

helpful-oil-7096

02/15/2024, 3:51 PM
yes i have checked it
f

fresh-fireman-491

02/15/2024, 3:54 PM
Try using it in the webchat
And check the logs here
h

helpful-oil-7096

02/15/2024, 3:55 PM
ok
there are no errors indicated
f

fresh-fireman-491

02/15/2024, 3:59 PM
Yea
And you used the exactly same code from Postman right?
h

helpful-oil-7096

02/15/2024, 4:00 PM
yes
this one works fine
f

fresh-fireman-491

02/15/2024, 4:01 PM
Can you send a screenshot of the updated code on Botpress
h

helpful-oil-7096

02/15/2024, 4:02 PM
f

fresh-fireman-491

02/15/2024, 4:03 PM
Try manually setting the feedbackData like you have in your postman
h

helpful-oil-7096

02/15/2024, 4:04 PM
ok
like this?
still nothing happens
l

limited-pencil-78283

02/16/2024, 12:34 AM
From the logs, there is no indication that axios called the endpoint. Can you put a console.log inside try before await to confirm the execution went inside try?
And I am sure you are calling the function within the execute card, right?
h

helpful-oil-7096

02/16/2024, 2:26 AM
yes i am using the execute card for the function
still no clue if it is error or what
l

limited-pencil-78283

02/16/2024, 5:16 AM
Can you share the full code in the card.
Did you check by adding logging within try before await?
There are no errors in the logs you shared
h

helpful-oil-7096

02/17/2024, 6:01 AM
THis is my code async function submitFeedbackToAPI() { const feedbackData = { user_name: user.user_name, user_email: user.user_email, user_feedback: user.user_feedback } const options = { url: 'https://bfe0-175-176-7-174.ngrok-free.app/api/submit_feedback', // Updated Ngrok URL method: 'POST', headers: { 'Content-Type': 'application/json' }, data: feedbackData } try { await axios(options) console.log('Feedback submitted successfully') } catch (error) { console.error('Error submitting feedback:', error) } }
my variables
f

fresh-fireman-491

02/17/2024, 6:14 AM
Good catch Lijo! Like Lijo said, you are not calling the function
You have to call the function at the end
h

helpful-oil-7096

02/17/2024, 6:15 AM
how can i call it sir?
f

fresh-fireman-491

02/17/2024, 6:15 AM
submitFeedbackToAPI()
h

helpful-oil-7096

02/17/2024, 6:17 AM
async function submitFeedbackToAPI() { const feedbackData = { user_name: user.user_name, user_email: user.user_email, user_feedback: user.user_feedback } const options = { url: 'https://bfe0-175-176-7-174.ngrok-free.app/api/submit_feedback', // Updated Ngrok URL method: 'POST', headers: { 'Content-Type': 'application/json' }, data: feedbackData } try { await axios(options) console.log('Feedback submitted successfully') } catch (error) { console.error('Error submitting feedback:', error) } } submitFeedbackToAPI(); // Calling the function
like this sir?
f

fresh-fireman-491

02/17/2024, 6:17 AM
Yea
h

helpful-oil-7096

02/17/2024, 6:18 AM
Thanks sir ill try running it
f

fresh-fireman-491

02/17/2024, 6:18 AM
Also Might want to exclude the URL so not everyone can call your API endpoint
h

helpful-oil-7096

02/17/2024, 6:19 AM
ow ok sir thanks
f

fresh-fireman-491

02/17/2024, 6:23 AM
Perfect! Thanks a lot for the help @limited-pencil-78283 , I am not sure I would have thought of that
l

limited-pencil-78283

02/17/2024, 6:23 AM
My pleasure @fresh-fireman-491
h

helpful-oil-7096

02/17/2024, 6:25 AM
still it does not work sir
i do not know why
f

fresh-fireman-491

02/17/2024, 6:26 AM
Any errors or anything else that could tell us why?
Any text that is underlined by red in your code ?
h

helpful-oil-7096

02/17/2024, 6:26 AM
ow i see one
i think my endpoint has an error
f

fresh-fireman-491

02/17/2024, 6:30 AM
Probably yes
But could you just try and publish your bot
And do it from the webchat
h

helpful-oil-7096

02/17/2024, 6:35 AM
i have tried it
Feb 17, 2024, 2:35:13 PM [d306823711] [bot-action] [flow:Main] [node:Standard3] [card:action]: Error submitting feedback: {"message":"Request failed with status code 404","name":"AxiosError","stack":"AxiosError: Request failed with status code 404\n at settle2 (/var/task/customer_code.js:124:1071)\n at IncomingMessage.handleStreamEnd (/var/task/customer_code.js:125:8878)\n at IncomingMessage.emit (node:events:529:35)\n at endReadableNT (node:internal/streams/readable:1400:12)\n at process.processTicksAndRejections (node:internal/process/task_queues:82:21)","config":{"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"adapter":["xhr","http"],"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","User-Agent":"axios/1.4.0","Content-Length":"74","Accept-Encoding":"gzip, compress, deflate, br"},"withCredentials":false,"baseURL":"","url":"https://bfe0-175-176-7-174.ngrok-free.app/api/submit_feedback","method":"post","data":"{\"user_name\":\"vince\",\"user_email\":\"vince@gmail.com\",\"user_feedback\":\"hai\"}"},"code":"ERR_BAD_REQUEST","status":404}
There is a problem in my Endpoint URL but when i use the postman the endpoint works fine but in the botpress it does not work
f

fresh-fireman-491

02/17/2024, 6:54 AM
Can you double check the path /api/submit_feedback
Make sure that there are no typos
Also
Just to make sure, the server is running right?
h

helpful-oil-7096

02/17/2024, 6:56 AM
like this sir?
f

fresh-fireman-491

02/17/2024, 6:58 AM
I can see that you have a ? At the end of the url in post man
And yes that should be running
Can you try and do another call from PostMan
And copy the url from it
h

helpful-oil-7096

02/17/2024, 6:59 AM
it work i call it and adds in my database
f

fresh-fireman-491

02/17/2024, 6:59 AM
From Botpress or PostMan?
h

helpful-oil-7096

02/17/2024, 7:00 AM
postman sir
f

fresh-fireman-491

02/17/2024, 7:01 AM
Then copy the url from that
And use it in Botpress
h

helpful-oil-7096

02/17/2024, 7:01 AM
sir it works
i am so happy
f

fresh-fireman-491

02/17/2024, 7:03 AM
Also from Botpress?
h

helpful-oil-7096

02/17/2024, 7:03 AM
yes sir
i followed your steps and it worked fine\
Thank you @fresh-fireman-491 @limited-pencil-78283 @future-psychiatrist-80851 and Botpress for helping me out
f

fresh-fireman-491

02/17/2024, 7:10 AM
That is amazing to hear!
h

helpful-oil-7096

02/17/2024, 9:32 AM
I have another question sir Is it possible to reset the populated variables after the user sends his/her feedback?
f

fresh-fireman-491

02/17/2024, 9:37 AM
Hey You can add an execute code card with user.feedback = " " Guessing that it's a string
h

helpful-oil-7096

02/17/2024, 9:40 AM
Ok sir i'll do it
Thank you sir it works very fine i separated the reset execute card
f

fresh-fireman-491

02/17/2024, 10:14 AM
Perfect! Feel free to create a new help post if you have any more questions
h

helpful-oil-7096

02/17/2024, 11:33 AM
Yes sir