I want my Dialogflow bot to send periodic messages through a Cronjob. I would do this by triggering an event through the /query endpoint. However, I am not sure how to get the sessionId. If I pass the last sessionId used, the response returns successfully, but I get no message in the conversation. Does the API support this pattern at all? All my sessionIds come from the same instance of the Facebook Messenger client
2 Answers
You should definitely use Facebook Messenger Broadcast API for this kind of functionality. This will broadcast your message to all users subscribed to the bot, you can execute this as a CRON job.
SessionID sending works but would get unwieldy down the line especially if you get a lot of users on the bot.
Caveats:
- You have to apply for this permission. (pages_messaging and pages_messaging_subscriptions.Takes about 1-2 days, but can test on Admin/Test users of the app)
- Each broadcast has to be a separate broadcast. (e.g. you can't send image and a text together, each has to be its own individual broadcast).
- Have some kind of un-subscription option as well. FB user might think you are spamming even if you clearly say in the messages that your bot will send updates.
- Use custom labels to create targetted sends. So you can either subdivide who you will send updates to about specific issues or just label people if they unsubscribe to your broadcast or not.
Basic workflow:
- Get permission to broadcast.
- Create message_creative_id via POST to endpoint
- Use message_creative_id to POST a broadcast_messages
- On a successful send you will get back broadcast_id

- 698
- 10
- 27
-
Thanks, that's an interesting solution and it answers the original question perfectly. However, I would prefer a way to trigger the Dialogflow event, so that when the user replies I can have a follow up intent be recognised as such rather than a top level intent – lzmartinico Mar 28 '18 at 23:19
-
That would be platform specific to Diagflow then. You'd need to ask their team on how to achieve that. – Komsomol Mar 29 '18 at 16:49
-
If you ask a question outside of the Dialogflow platform, how would Dialogflow understand the context of the user's reply? How would it know which intent it is relevant to? – Tom Jul 27 '19 at 03:53
-
@Tom I would ask this in another Stackoverflow question as it's outside of the solution needed here. But basically you carry conversations in linear flow so your app logic understands the context. Eg. Order ticket -> What date -> Pick location etc. Multi branches conversational trees would be a nightmare to make, you'd need to tag every reply and program logic to be flexible with responses. – Komsomol Jul 29 '19 at 15:37
Dialogflow doesn't support proactive messaging at this time and session IDs are generated by Dialogflow and are only active for 20min after the last message is sent.
The only way to send messages proactively to your users would be to use the same Facebook Messenger authentication information you've configured in Dialogflow to send a Facebook message to the user you'd like to notify. When the user responds the request will go to Dialogflow and your Dialogflow agent will respond and continue the conversation.

- 3,429
- 16
- 16