如何使用python在MessengerDialogflow chatbot中将Mysql查询结果作为Excel报表发送

2024-04-26 10:10:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python程序作为webhook创建了一个带有dialogflow的messenger聊天机器人。在一个用例中:用户通过给出周数(1周/2周等)来请求每周报告。Python程序不需要几个星期作为参数,将结果以csv的形式保存在本地。我想把csv作为聊天机器人的附件发送给用户。你知道吗

从现在起,我保存在我的本地驱动器与谷歌驱动器同步csv文件,并发送链接给用户。每次用户需要不同周的销售文件更新。但在实际场景中,当多个用户同时请求报告时,其中一个用户得到了错误的文件(不管最新运行的查询是什么)。你知道吗

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

Tags: 文件csv用户from程序report报告机器人
1条回答
网友
1楼 · 发布于 2024-04-26 10:10:10

在做了更多的研究之后,我找到了一种共享正确文件的方法。步骤如下:

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

  2. 并将此文件保存到AWS S3存储桶中。

  3. 使用文件名创建链接,然后与用户共享。你知道吗

相关问题 更多 >