I have created a messenger chatbot with dialogflow using Python program as webhook. In one of use case: User ask for weekly report by giving no of weeks (1 week/2 week etc). Python program take no of weeks as parameters an save the result in my local as csv. I want to send that csv to user as attachment on chatbot.
As of now I am saving the csv files in my local drive which is sync with google drive and send the link to user. Every time user want different weeks sales file updates. But when in real life scenario multiple user asked for report at same time, one of the user get the wrong file (Whatever query ran in latest).
inte = get_parameter_from_req(req)
week_num = round(inte)
print(week_num)
#cusid = pd.DataFrame([inte],columns = ['Weeks'])
#cusid.to_csv('C:/Users/Jon/Desktop/Reporting BOT/Reporting Bot Code/num_of_weeks.csv',index = False)
print("Inside DB Test")
mydb = mysql.connector.connect(host="localhost",user="root",passwd="****",database = "jon_bp")
mycursor = mydb.cursor()
mycursor.execute("select * from trans where t_date between curdate()-interval %s week AND curdate()",(week_num,))
result = mycursor.fetchall()
dataf = pd.DataFrame(result)
dataf.columns = ['Name','Amount','t_date']
dataf.to_csv('C:/Users/jon/Desktop/Reporting BOT/Report on cloud/Weekly Report by Bot.csv',index = False)
response = {
'fulfillmentText': 'Hi! Sales report for {0} weeks is ready.'.format(str(week_num)) +"\n"+ 'You can download the report from here: fix url for report',
}
After doing some more research, I have found a way to share the correct file. Below are the steps:
I am appending timestamp in file name (including micro second)
filename = 'my_file_{0}.csv'.format(datetime.now().strftime("%Y%m%d_%H%M%S%f")
This time and saving this file to AWS S3 storage bucket.