'Messenger bot send a message every 5th message
I am making a simple bot for messenger on python using fbmq, that handles quick questions.
I made bot to send a message when users text to my Facebook page out of my working hours.
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!"
Since people normally express what they want in more than 1 message, they got their chat spammed with "We are closed message!"
I didn't like that that much, because I want to give a chance for user to leave a message so I can answer tomorrow.
Do you know any way I can make bot send "We are closed!" every 5th message sent by the user?
I tried this:
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
But it didn't work. If you have any ideas how can I realize that I will be really thankful :)
// I tried a while loop.
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
But it didn't work. Recieved an error by Facebook deliviring messages to the bot.
Solution 1:[1]
Well I didn't find a better solution for this.
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
I would want to make it with timestamp now, but honestly don't know how. So for example if there is a user you didn't text anything for a long time would receive a "Welcome back, we missed you" message.
I am not sure how this code will work with people texting at the same time neither. Probably all buggy, since I use a global variable. If you have any ideas please help :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Aleksandr Moroz |