gpt4 book ai didi

python - 如何使用 python 在 Messenger-Dialogflow 聊天机器人中将 Mysql 查询结果作为 Excel 报告发送

转载 作者:行者123 更新时间:2023-11-29 16:08:41 25 4
gpt4 key购买 nike

我使用 Python 程序作为 webhook 创建了一个带有对话流的信使聊天机器人。在其中一种用例中:用户通过给出周数(1 周/2 周等)来请求每周报告。 Python 程序不需要几周作为参数,并将结果以 csv 格式保存在本地。我想将该 csv 作为聊天机器人上的附件发送给用户。

到目前为止,我正在将 csv 文件保存在本地驱动器中,该驱动器与 Google 驱动器同步并将链接发送给用户。每次用户想要不同周的销售文件更新时。但在现实生活中,当多个用户同时请求报告时,其中一个用户会得到错误的文件(无论最新运行的是什么查询)。

        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',
}

最佳答案

经过更多研究,我找到了一种共享正确文件的方法。以下是步骤:

  1. 我在文件名中附加时间戳(包括微秒) filename = 'my_file_{0}.csv'.format(datetime.now().strftime("%Y%m%d_%H%M%S%f")

  2. 这次将此文件保存到 AWS S3 存储桶。

  3. 借助文件名创建链接,然后与用户共享。

关于python - 如何使用 python 在 Messenger-Dialogflow 聊天机器人中将 Mysql 查询结果作为 Excel 报告发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55495791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com