Closed josephktcheung closed 6 years ago
Here's another example.
'use strict';
var restify = require('restify');
var builder = require('botbuilder');
var server = restify.createServer();
const axios = require('axios');
require('dotenv').config();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// setup bot credentials
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// Bot Storage: Here we register the state storage for your bot.
// Default store: volatile in-memory store - Only for prototyping!
// We provide adapters for Azure Table, CosmosDb, SQL Azure, or you can implement your own!
// For samples and documentation, see: https://github.com/Microsoft/BotBuilder-Azure
var inMemoryStorage = new builder.MemoryBotStorage();
var bot = new builder.UniversalBot(connector).set('storage', inMemoryStorage); // Register in memory storage
// send simple notification
function sendProactiveMessageFromBotBuilder(address) {
var msg = new builder.Message().address(address);
msg.text('Hello, this is a notification from botbuilder');
msg.textLocale('en-US');
bot.send(msg);
}
function sendRawProactiveMessage(address) {
axios.post(`https://graph.facebook.com/v2.6/me/messages?access_token=${process.env.PAGE_ACCESS_TOKEN}`, {
recipient: {
id: address.user.id,
},
message: {
text: 'Hello, this is a notification from direct POSTing'
},
});
}
var savedAddress;
server.post('/api/messages', connector.listen());
// Do GET this endpoint to delivey a notification
server.get('/api/CustomWebApi', (req, res, next) => {
sendProactiveMessage(savedAddress);
res.send('triggered');
next();
}
);
// root dialog
bot.dialog('/', function (session, args) {
savedAddress = session.message.address;
var message = 'Hello! In 10 seconds I\'ll send you a message proactively to demonstrate how bots can initiate messages.';
session.send(message);
message = 'You can also make me send a message by accessing: ';
message += 'http://localhost:' + server.address().port + '/api/CustomWebApi';
session.send(message);
setTimeout(() => {
console.log('botbuilder message sent');
sendProactiveMessageFromBotBuilder(savedAddress);
// sendRawProactiveMessage(savedAddress);
setTimeout(() => {
console.log('raw message sent');
sendRawProactiveMessage(savedAddress);
}, 5000);
}, 5000);
});
node index.js
, connect the azure bot to a Facebook pageExpect to get 2 mobile native notifications from Facebook Messenger when proactive message arrives.
Only 1 notification is triggered (the one sent by axios)
Hi @josephktcheung ! As far as I know, the only way to currently trigger the notification to users is to send a card through the bot to them. I'm looking into other possibilities at the moment.
@jwiley84 thanks for investigating this. Do you know why only card type message works but others do not? Is it because of the payload sent by MS to Facebook?
I belive so, yes, though I'm not 100% sure, and my facebook channel guru is on vacation. As soon as he returns, I'll update this comment.
Bot Info
Issue Description
We faced a strange issue recently that messages sent from bot builder don't trigger notification on Facebook Messenger.
Normally when there's a new message coming from the bot and if Facebook Messenger is not the currently active app, the mobile device should receive a notification. However, this mechanism seems to be broken when we send proactive message from the bot to Facebook Messenger through Microsoft Bot Framework recently, the message can reach the user's device but no native notification is triggered. It used to work before (successfully triggering a notification when a proactive message is sent from our Botbuilder powered bot to our users' mobile devices)
We tested it by sending a POST request using Postman to Facebook with a basic message payload and it could trigger the notification without any problem. Thus we think that the payload sent from Microsoft Bot Framework to Facebook may be malformed. However we don't know the exact payload sent by Microsoft to Facebook so it's very hard for us to debug this issue.
We've also created a bug report on Facebook developers as well but because this bug now seems to be caused by this library, a library not created by Facebook, we don't think Facebook will offer much help. This bug could be introduced by Facebook, but since we don't know the JSON sent by MS to Facebook, there's no way to know how to fix this.
@JasonSowers please help investigate the payload sent from MS and we desperately want feature request #4858 Ability to inspect JSON sent to channel so that we know what's going on.
Code Example
Basically same as botbuilder-samples'
simpleSendMessage
, only changed timeout second from 5 to 10 seconds and added a log message for illustration.Reproduction Steps
node index.js
, connect the azure bot to a Facebook pageExpected Behavior
Expect to get mobile native notification from Facebook Messenger when proactive message arrives. Here I send a POST request to Facebook with following json format, and you can see that I receive a notification after the request is sent while Facebook Messenger is in background:
Actual Results
After 10 seconds, a new message arrives, but it doesn't show a mobile native notification from Facebook Messenger, instead it's changed silently (unread message badge increments to "1")