【问题标题】:Messenger bot send a message every 5th messagepython 上的 Messenger 机器人
【发布时间】:2018-12-10 19:15:00
【问题描述】:

我正在使用 fbmq 为 python 上的 Messenger 制作一个简单的机器人,可以处理快速问题。

当用户在我的工作时间之外向我的 Facebook 页面发短信时,我制作了机器人来发送消息。

working_hours = [12,13,14,15,16]

if messaging_event.get('message') and (now.hour no in working_hours):
   page.send(sender_id, "Sorry we are closed!"

由于人们通常在不止一条消息中表达他们想要的东西,因此他们的聊天中会出现“我们已关闭消息!”的垃圾邮件

我不太喜欢那个,因为我想给用户一个机会留言,以便我明天回答。

你知道有什么方法可以让机器人发送“我们关门了!”用户每发送 5 条消息?

我试过了:

count = 0
if (count / 3 == 1):
  page.send(sender_id, "Sorry we are closed!")
  count = 0
if messaging_event.get('message') and (now.hour no in working_hours):
  count += 1

但它没有用。如果您有任何想法,我怎么能意识到我会非常感激:)

// 我尝试了一个while循环。

for messaging_event in messaging:

        sender_id = messaging_event['sender']['id']
        recipient_id = messaging_event['recipient']['id']

        messaging_event.get('postback'):
        messaging_event.get('message'):

        while (now.hour not in working_hours):
            count = 0
            if (count/3 == 1):
                page.send(sender_id, "Hello")

            if messaging_event.get('message'):
                count += 1

但它没有用。 Facebook 向机器人发送消息时收到错误。

【问题讨论】:

    标签: python python-3.x flask bots messenger


    【解决方案1】:

    好吧,我没有找到更好的解决方案。

    count = 0
    working_hours = [ 8... ]
    
    @app.route('/', methods=['POST'])
    def webhook():
        print(request.data)
        data = request.get_json()
    
        if data['object'] == "page":
            entries = data['entry']
    
            for entry in entries:
                messaging = entry['messaging']
    
                for messaging_event in messaging:
    
                    sender_id = messaging_event['sender']['id']
                    recipient_id = messaging_event['recipient']['id']
    
                    if now.hour not in working_hours:
                        global count
                    if (count == 3):
                        page.send(sender_id, "We are closed!")
                    if (count == 4):
                        count = 0
                    if messaging_event['message'].get('text'):
                        count += 1
    

    我现在想用时间戳来制作它,但老实说不知道怎么做。因此,例如,如果有一个用户您很长时间没有发短信,则会收到“欢迎回来,我们想念您”的消息。

    我不确定这段代码如何与同时发短信的人一起工作。可能都是错误的,因为我使用了一个全局变量。如果您有任何想法,请帮助:)

    【讨论】:

      猜你喜欢
      • 2017-01-06
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多