i cannot connect my bot to my MySQL database serve...
# 🤝help
h
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
hey @helpful-oil-7096 Please utilize your server's public URL in Botpress to populate data in the database.
h
Good day, you mean i'll create an API endpoint?
f
yes
h
Sorry, I am new to web development. Can you please clarify how to do it? I still cannot understand the terms.
f
can you explain how to use database in botpress?
h
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
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
Thank you for helping me out
f
In which technologies do you work?
h
I do not have any, i'm at college as a graduating student
l
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
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
Which errors are you getting?
h
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
Utilize ngrok to expose your local URL as a public URL.
h
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
Can you try and publish your bot and try from the webchat?
h
I have tried it but still it is not working
f
And its still not giving any errors on either side?
Have you checked the logs for the published version
h
yes i have checked it
f
Try using it in the webchat
And check the logs here
h
ok
there are no errors indicated
f
Yea
And you used the exactly same code from Postman right?
h
yes
this one works fine
f
Can you send a screenshot of the updated code on Botpress
h
f
Try manually setting the feedbackData like you have in your postman
h
ok
like this?
still nothing happens
l
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
yes i am using the execute card for the function
still no clue if it is error or what
l
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
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
Good catch Lijo! Like Lijo said, you are not calling the function
You have to call the function at the end
h
how can i call it sir?
f
submitFeedbackToAPI()
h
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
Yea
h
Thanks sir ill try running it
f
Also Might want to exclude the URL so not everyone can call your API endpoint
h
ow ok sir thanks
f
Perfect! Thanks a lot for the help @limited-pencil-78283 , I am not sure I would have thought of that
l
My pleasure @fresh-fireman-491
h
still it does not work sir
i do not know why
f
Any errors or anything else that could tell us why?
Any text that is underlined by red in your code ?
h
ow i see one
i think my endpoint has an error
f
Probably yes
But could you just try and publish your bot
And do it from the webchat
h
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
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
like this sir?
f
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
it work i call it and adds in my database
f
From Botpress or PostMan?
h
postman sir
f
Then copy the url from that
And use it in Botpress
h
sir it works
i am so happy
f
Also from Botpress?
h
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
That is amazing to hear!
h
I have another question sir Is it possible to reset the populated variables after the user sends his/her feedback?
f
Hey You can add an execute code card with user.feedback = " " Guessing that it's a string
h
Ok sir i'll do it
Thank you sir it works very fine i separated the reset execute card
f
Perfect! Feel free to create a new help post if you have any more questions
h
Yes sir