Facebook messenger api - Metadata on the message is not coming back in the response - facebook

I have following code to send text message with metadata. When user responds with text, metadata field on the message is empty. Is it a bug or Messenger api does not support this functionality?
function sendTextMessage(recipientId, messageText, metadata) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: messageText,
metadata: metadata,
}
};
callSendAPI(messageData);
}
function callSendAPI(messageData) {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: PAGE_ACCESS_TOKEN },
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
if (messageId) {
console.log("FBHook Successfully sent message with id %s to recipient %s",
messageId, recipientId);
} else {
console.log("FBHook Successfully called Send API for recipient %s",
recipientId);
}
} else {
console.error(response.error);
}
});
}

This is not how it is expected to behave. The metadata field will be returned to webhook immediately if subscribed to the "message_echoes" field. This is meant for co-ordination between multiple apps linked to the page.
From the changelog - https://developers.facebook.com/docs/messenger-platform/changelog
"New field: metadata, passed from the Send API and sent to the message_echoes callback, to help interoperability betwen multiple bots."

Related

Azure communication services

I am using azure communication services in my react app to send email.
But It is giving CORS error
import { EmailClient } from "#azure/communication-email";
function App() {
const connectionString =
"**************************************************************************************";
const client = new EmailClient(connectionString);
const sender = "1000055393#hexaware.com";
const emailContent = {
subject: "Send email quick start test- JS sample",
plainText:
"Test Email from JS Send Email Sample Application\n\n This email is part of testing of email communication service. \\n Best wishes",
html: "<html><head><title>ACS Email as a Service</title></head><body><h1>ACS Email as a Service - Html body</h1><h2>This email is part of testing of email communication service</h2></body></html>",
};
const toRecipients = {
to: [{ email: "krnsda04#gmail.com", displayName: "Karan S" }],
};
async function main() {
try {
const emailMessage = {
sender: sender,
content: emailContent,
recipients: toRecipients,
};
console.log(sender,"sender");
const sendResult = await client.send(emailMessage);
console.log(sendResult,"result");
if (sendResult && sendResult.messageId) {
// check mail status, wait for 5 seconds, check for 60 seconds.
const messageId = sendResult.messageId;
if (messageId === null) {
console.log("Message Id not found.");
return;
}
console.log("Send email success, MessageId :", messageId);
let counter = 0;
const statusInterval = setInterval(async function () {
counter++;
try {
const response = await client.getSendStatus(messageId);
if (response) {
console.log(
`Email status for {${messageId}} : [${response.status}]`
);
if (response.status.toLowerCase() !== "queued" || counter > 12) {
clearInterval(statusInterval);
}
}
return;
} catch (e) {
console.log("Error in checking send mail status: ", e);
}
}, 5000);
} else {
console.error(
"Something went wrong when trying to send this email: ",
sendResult
);
}
return;
} catch (e) {
console.log(
"################### Exception occoured while sending email #####################",
e
);
}
}
main();
return <h3>hello ${connectionString}</h3>;
}
export default App;
But when running this code, I m getting this error.
Access to XMLHttpRequest at 'https://************************/emails:send?' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
In Azure docs, they have given the code in Node.js.
Since Iam using that code in react and accessing from front end, is it showing CORS error?
How to solve this issue?
As you can see in the SDKs and REST APIs docs, ACS Email library is meant to be used in a trusted service environment. This is because there is no reasonable way to protect your connection string in the browser.
In your case, building a Node.js backend and letting your React app talk to it would be a good and preferred solution.

Payrexx integration in flutter webview

As described here https://developers.payrexx.com/docs/mobile-apps-javascript
I would like to interact with the javascript events of an iframe I want to create in the webview_flutter plugin.
The following example code is given in the official documentation
window.addEventListener('message', handleMessage(this), false);
and
function handleMessage(e) {
if (typeof e.data === 'string') {
try {
var data = JSON.parse(e.data);
} catch (e) {}
if (data && data.payrexx) {
jQuery.each(data.payrexx, function(name, value) {
switch (name) {
case 'transaction':
if (typeof value === 'object') {
if (value.status === 'confirmed') {
//handling success
} else {
//handling failure
}
}
break;
}
});
}
}
}
Do you know a way to do this? I have implemented an iframe in which there is the address of my gateway, but it is impossible to check if the payment has taken place.
Sounds good. The Payrexx iFrame sends a post message with the transaction details (including transaction status) to the parent window (e.g. your Flutter webview) after the payment (on the Payrexx result page). So you only need to add an event listener for type "message" in your webview as in the example:
window.addEventListener('message', handleMessage(this), false);
Please make sure you also send a post message into the Payrexx iFrame as soon as the iFrame is loaded (onload event):
let iFrame = document.getElementById('IFRAME-ID');
if (iFrame) {
iFrame.contentWindow.postMessage(
JSON.stringify({
origin: window.location.origin,
}),
iFrame.src,
);
}
Now you are ready to receive and handle the messages from the Payrexx iFrame:
private handleMessage(e): void {
try {
let message = JSON.parse(e.data);
if (typeof message !== 'object' ||
!message.payrexx ||
!message.payrexx.transaction) {
return;
}
let transaction = message.payrexx.transaction;
console.log(transaction);
} catch (e) {
}
};
Last but not least:
Make sure you also check the transaction status via transaction webhook (server-to-server notification):
https://docs.payrexx.com/developer/guides/webhook

Pushserver isn't waiting for promise answer

I have this push server which sends push notifications to devices and logs the response to a json. The code goes:
push.js
module.exports.sendPushSubscription = async (post, recipient, p256, auth) => {
console.log("Mandando PUSHES");
const subscription = {
endpoint: `https://fcm.googleapis.com/fcm/send/${recipient}`,
expirationTime: null,
keys: {
p256dh: `${p256}`,
auth: `${auth}`
}
}
console.log(subscription);
error = webpush.sendNotification(subscription, JSON.stringify(post))
.then(() => {
console.log("Notificación enviada");
return "Sent!";
})
.catch(err => {
return err;
});
await Promise.all(error);
return error;
};
routes.js
recipients = recipients.split(',');
p256 = p256.split(',');
auth = auth.split(',');
let err = "";
for(var i=0;i<recipients.length;i++) {
err = "Trying";
err = push.sendPushSubscription(post, recipients[i], p256[i], auth[i]);
post.recipients.push({
recipient: recipients[i],
error: err
})
}
rsp.json(post);
However, despite the code might suggest the sendPushSubscription function should wait for the promises to end (it's usually only one) and return either Sent! or the error itself, the json always adds the recipient and an empty error. No matter if it succeeds or fails, the answer is:
"recipients": [
{
"recipient": "d96UkANUtAo:APA91bF1-...",
"error": {}
}
Any ideas? Thanks in advance.
Best regards,
Emiliano

Web App Bot throws invalid status code 'Unauthorized' on Facebook Channel

I'm developing a Facebook with the Microsoft.Bot.Builder Framework (4.2.2)
The Bot Works on emulator and on Azure with the "Test in Web Chat" Really fine. But on the Facebook Channel I get an "There was an error sending this message to your bot: HTTP status code InternalServerError"
My log stream says:
019-04-12 13:23:22.154 +00:00 [Information]
Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting
HTTP/1.1 POST
http://thomasreigltestbot1995.azurewebsites.net/api/messages
application/json; charset=utf-8 688
2019-04-12 13:23:22.155 +00:00 [Information]
Microsoft.Bot.Builder.Integration.IAdapterIntegration: Received an
incoming activity. ActivityId: Vg2USjaJGCZ1Ib_-
LTftnL9F6qg_DCJaV3XbIqGzSpisuO10eYLezkjHJxSVcYAAUWQvynDJbUyfvyBgN30B3w
2019-04-12 13:23:22.155 +00:00 [Information]
Microsoft.Bot.Builder.Integration.IAdapterIntegration: Sending
activity. ReplyToId: Vg2USjaJGCZ1Ib_-
LTftnL9F6qg_DCJaV3XbIqGzSpisuO10eYLezkjHJxSVcYAAUWQvynDJbUyfvyBgN30B3w
2019-04-12 13:23:22.388 +00:00 [Error]
Facebook_Events_Bot.FacebookEventsBot: Exception caught :
Microsoft.Bot.Schema.ErrorResponseException: Operation returned an
invalid status code 'Unauthorized'at
Microsoft.Bot.Connector.Conversations.
This is my simple Bot from this repo:
https://github.com/Microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/23.facebook-events
public class FacebookEventsBot : IBot
{
private const string DialogId = "question";
/// </summary>
private readonly DialogSet _dialogs;
private Tuple<int, string> tuple;
public FacebookEventsBot(BotAccessors accessors)
{
if (accessors == null)
{
throw new ArgumentNullException(nameof(accessors));
}
_dialogs = new DialogSet(accessors.ConversationDialogState);
_dialogs.Add(new AttachmentPrompt(DialogId));
}
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
const string facebookPageNameOption = "Facebook Page Name";
const string quickRepliesOption = "Quick Replies";
const string postBackOption = "PostBack";
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
// Check if we are on the Facebook channel.
if (turnContext.Activity.ChannelId == Channel.Channels.Facebook)
{
// Analyze Facebook payload from channel data.
ProcessFacebookPayload(turnContext.Activity.ChannelData);
// Initially the bot offers to showcase 3 Facebook features: Quick replies, PostBack and getting the Facebook Page Name.
// Below we also show how to get the messaging_optin payload separately as well.
switch (turnContext.Activity.Text)
{
// Here we showcase how to obtain the Facebook page name.
// This can be useful for the Facebook multi-page support provided by the Bot Framework.
// The Facebook page name from which the message comes from is in turnContext.Activity.Recipient.Name.
case facebookPageNameOption:
{
var reply = turnContext.Activity.CreateReply($"This message comes from the following Facebook Page: {turnContext.Activity.Recipient.Name}");
await turnContext.SendActivityAsync(reply);
break;
}
// Here we send a HeroCard with 2 options that will trigger a Facebook PostBack.
case postBackOption:
{
var dialogContext = await _dialogs.CreateContextAsync(turnContext, cancellationToken);
var results = await dialogContext.ContinueDialogAsync(cancellationToken);
var card = new HeroCard
{
Text = "Is 42 the answer to the ultimate question of Life, the Universe, and Everything?",
Buttons = new List<CardAction>
{
new CardAction() { Title = "Yes", Type = ActionTypes.PostBack, Value = "Yes" },
new CardAction() { Title = "No", Type = ActionTypes.PostBack, Value = "No" },
},
};
var reply = turnContext.Activity.CreateReply();
reply.Attachments = new List<Attachment> { card.ToAttachment() };
await turnContext.SendActivityAsync(reply);
break;
}
// By default we offer the users different actions that the bot supports, through quick replies.
case quickRepliesOption:
default:
{
var reply = turnContext.Activity.CreateReply("What Facebook feature would you like to try? Here are some quick replies to choose from!");
reply.SuggestedActions = new SuggestedActions()
{
Actions = new List<CardAction>()
{
new CardAction() { Title = quickRepliesOption, Type = ActionTypes.PostBack, Value = quickRepliesOption },
new CardAction() { Title = facebookPageNameOption, Type = ActionTypes.PostBack, Value = facebookPageNameOption },
new CardAction() { Title = postBackOption, Type = ActionTypes.PostBack, Value = postBackOption },
},
};
await turnContext.SendActivityAsync(reply);
break;
}
}
}
else
{
// Check if we are on the Facebook channel.
if (turnContext.Activity.ChannelId == Channel.Channels.Facebook)
{
// Here we can check for messaging_optin webhook event.
// Facebook Documentation for Message optin:
// https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_optins/
}
await turnContext.SendActivityAsync($"Received activity of type {turnContext.Activity.Type}.");
}
}
private void ProcessFacebookPayload(object channelData)
{
// At this point we know we are on Facebook channel, and can consume the Facebook custom payload
// present in channelData.
var facebookPayload = (channelData as JObject)?.ToObject<FacebookPayload>();
if (facebookPayload != null)
{
// PostBack
if (facebookPayload.PostBack != null)
{
OnFacebookPostBack(facebookPayload.PostBack);
}
// Optin
else if (facebookPayload.Optin != null)
{
OnFacebookOptin(facebookPayload.Optin);
}
// Quick reply
else if (facebookPayload.Message?.QuickReply != null)
{
OnFacebookQuickReply(facebookPayload.Message.QuickReply);
}
// TODO: Handle other events that you're interested in...
}
}
private void OnFacebookOptin(FacebookOptin optin)
{
// TODO: Your optin event handling logic here...
}
private void OnFacebookPostBack(FacebookPostback postBack)
{
// TODO: Your PostBack handling logic here...
}
private void OnFacebookQuickReply(FacebookQuickReply quickReply)
{
// TODO: Your quick reply event handling logic here...
}
}
}

How to retrieve `custom_disclaimer_responses` in Facebook lead gen webhook data

I have set up a webhook that gets data submitted from a lead gen ad on Facebook.
In my response I have access to field_data and can see names and email address coming through but can't seem to find where the custom_disclaimer_responses is.
I am using the graph API explorer to send test submissions and getting a successful response
My webhook code is as follows:
exports.webhook = function (req, res, next) {
var lead = req.body.entry[0].changes[0].value;
var leadID = lead.leadgen_id;
var formID = lead.form_id;
var customDisclaimerResponses = lead.custom_disclaimer_responses
fs.readFile(config.token, 'utf8', function(err, data) {
if (err) {
console.log('err', err)
throw err;
}
var content = JSON.parse(data);
if(!content.access_token) {
console.log('Facebook Access Token is invalid.');
res.sendStatus(400);
} else {
FB.options({accessToken: content.access_token});
FB.api('/' + leadID, function (response) {
if(response && response.error) {
console.log('error', response.error);
res.sendStatus(400);
} else {
var fields = response.field_data;
// do stuff here with fields
// Response moved to outside of above function block since Facebook will
// stop sending updates if the webhook starts giving errors repeatedly.
res.sendStatus(200);
}
});
}
});
}
Example of response:
{ created_time: '2016-11-17T09:52:44+0000',
id: '<id>',
field_data:
[ { name: 'email', values: [Object] },
{ name: 'first_name', values: [Object] },
{ name: 'last_name', values: [Object] },
{ name: 'city', values: [Object] },
{ name: 'date_of_birth', values: [Object] }
]
}
I don't use webhooks, but I think this can help you:
You can add the parameter fields=custom_disclaimer_responses to get the data you need.
I re-join collected data (the ones in field_data got without parameter) by user id
This is my PHP code, for example:
$url = "https://graph.facebook.com/v2.9/$leadForm/leads?access_token=".$appToken;
$urlCustom = "https://graph.facebook.com/v2.9/$leadForm/leads?fields=custom_disclaimer_responses&access_token=".$appToken;