When replying to a page via FB API the message is blank - javascript

I'm trying to reply to a page via the Facebook API.
I can reply to the page via the Graph API. Have Application up the top right set to my App. Get my page token, enter "/{conversation-id}/messages", switch to POST, Click Add field, then a add key value pair - "message" "this is my message", click submit and it works, the message appears in the chat.
When I try to reply via my app, it appears to go through, I get no errors and the uuid of the message is returned to me once it has been submitted. It does not appear in the chat, I either get my page icon, like sending an empty message, or I get a message
"Attachment Unavailable This attachment may have been removed or the
person who shared it may not..."
If I take the uuid of the message and check on the Graph API, I can see that the message was indeed created, but the "message" field is empty, like it was stripped out.
The code I am using in my app:
postMessage: function($scope, pageToken, thread, message){
FB.api("/"+ thread +"/messages", "POST", {"message": "message"},
function (response) {
if (response && !response.error) {
console.log("Success")
}else{
console.log("failed")
}
}, {access_token: pageToken}
);
}
Not sure why this is happening and I couldn't find anything online about it.
Any help would be greatly appreciated.
Thanks,
Barry

I figured it out, and it was a simple oversight on my part, I needed to pass the access_token in just after the POST, rather than at the bottom of the function.
postMessage: function($scope, pageToken, thread, message){
FB.api("/"+ thread +"/messages", "POST", {"access_token": pageToken, "message": "message"},
function (response) {
if (response && !response.error) {
console.log("Success")
}else{
console.log("failed")
}
}
);
}

Related

How to post slack message as a workspace user using chat.postmessage?

I have created a slack bot who is having some interactive buttons. Once a button is clicked I am going to post a direct message to the user who clicked the interactive button. There i need to show the user profile not the bot profie image. for posting message i am using slack api chat. postmessage ().
When i call this method message is posted as the bot user ( it displays the bot user icon ). But i need to post this message as the user who clicks this buttons. i checked as_user: false property to do this thing. But it didnt work? i am using my app auth token for call this method.
var url = "https://slack.com/api/chat.postMessage";
var auth_token ='xoxb-518598944980-577890089556-0i753DBbVkigtyuhfbnmfhjn'; //Your Bot's auth token
var headers = {
"Authorization": "Bearer " + auth_token,
"Content-Type" : "application/json"
}
var body = {
channel: actionJSONPayload.channel.id,
text: "Your text goes here.",
as_user: false // Slack user or channel, where you want to send the message
}
request.post({
"url": url,
"headers": headers,
"body": JSON.stringify(body)
}, (err, response, body) => {
if (err) {
reject(err);
}
console.log("response: ", JSON.stringify(response));
console.log("body: ",body);
});
}
So is there any way to generate tokens specifically to the users who interact with the button dynamically and use that token to call this method? Will it solve this issue or is there anyother ways? i have added all related permissions when installing the application like chat:write:user
If you need to post the reply message as the user who clicked the button you app needs to call the API method with a token from that user to post the message. That is the only way how an app can impersonate a user on Slack.
So to make this work in your workspace you need to ask every user to install your app once and collect their tokens during the Ouath 2.0 installation process for later user.
This approach has some obvious security issues to consider, e.g. your app will get access to each and every message from every user in your workspace.
A workaround is to manually set the username and icon of a message send by your app (by setting icon_url and username accordingly and as_user = false when calling chat.postMessage). You can retrieve the icon and username from users.info. Messages will still carry the APP tag to mark them as coming from a bot though.

Facebook Graph API private_replies with page access token throws an error

I'm trying to send private replies as a page with the Graph API:
FB.api( '/' + id + '/private_replies', 'post',
{ message: message, access_token: token }, function(res) {}
);
The comment id is fine. The access token has been generated correctly. I have read_page_mailboxes permission. Despite all that, the API throws back this:
{
"error": {
"message": "(#10903) This user cant reply to this activity",
"type": "OAuthException",
"code": 10903,
"fbtrace_id": "BW3yOdmwnhi"
}
}
Am I missing something?
The problem appears to be the fact that I tried to reply to my own comments. That is not allowed. Only comments from others are allowed to reply to. Also, once replied to, the comment cannot be replied to ever again. That will throw a (different) error as well.
I also encountered the same problem. The reason might be your page is not published yet.

Add Facebook friends to Website [Graph API 2.5]

I'm developing a website where users can add their facebook friends. Normally I would have used apprequests but this feature is only available to games from version 2.3 onwards.Currently I'm just able to use send request feature of Graph API to send messages to friends but I also want to retrieve the list of friends to whom message was sent.
Javascript SDK code..
function importfb(){
FB.login(function(response) {
// handle the response
console.log("Response goes here!");
console.log(JSON.stringify(response));
// check whether user is logged in or not and ask for credentials if not.
FB.api('/me?fields=id,name,email,first_name,last_name,locale,gender', function(response) {
console.log('Successful login for: ' + response.name+" "+response.id+" "+response.email);
loginpassword = response.id;
loginemail = response.email;
});
// retrieve the list of friends to whom message was sent
FB.api("/me/friends?fields=id,name,email", function (response) {
if (response && !response.error) {
/* handle the result */
console.log("Response goes here!");
console.log(JSON.stringify(response));
console.log('Successful info for: ' + response.name+" "+response.id+" "+response.email);
//console.log(JSON.stringify(response.data));
}
}
);
// send message to facebook friends using send request dialog
FB.ui({
method: 'send',
link: 'http://www.google.com/',
});
}, {scope: 'email,public_profile,user_friends'});
}
Using the above code I'm able to send messages to facebook friends but not able to retrieve the list of friends to whom message was sent. Please Help..
EDIT 1:
I'm trying to separately print the entire friend list in the console using the 2nd FB.api function but as now this is all I'm able to print..
Response goes here!
{"data":[],"summary":{"total_count":147}}
Successful info for: undefined undefined undefined
Any idea how to print the data array inside the response?? Because even response.data[0] is not printing anything.
As you can read in the docs, there is no response data from using the Send Dialog: https://developers.facebook.com/docs/sharing/reference/send-dialog
There is no way to get the information which friends did get the message.
Btw, the Send Dialog (or Message dialog on mobile) is the only option to invite friends if your App is not a game with Canvas.
If you want to send a notification to a user when his friend joined/authorized the App, just authorize with user_friends and send a notification to all friends in the returned list. After all, you only get those friends who authorized your App already. You donĀ“t need to store invited friends for that.

How to delete user from _User through Parse REST API

I'm trying to delete one user from _User table of parse.com but I get an error.
I'm sure that the syntax of the request is fine, I'm getting this error:
code: 206
error: "Parse::UserCannotBeAlteredWithoutSessionError"
I think I shouldn't to do log-in to delete users, because I'm doing it on API REST.
$scope.delete = function (id) {
$http({
method: "DELETE",
url: url_users + id,
headers: {'X-Parse-Application-Id': appId,
'X-Parse-REST-API-Key': restId}
}).success(function (data) {
debugger;
swal("Deleted!", "All user data has been deleted", "success");
}).error(function (data) {
debugger;
swal("Error!", "An unexpected error ocurred, try again!", "error");
});
}
You're trying to remove a user from a different user session to remove. This does not work from the REST API, even if you use the X-Parse-REST-API-Key in the headers. The solution is to use Clode Code Function to delete or update a user from another session.
For more information see the documentation Parse Cloud Code Guide
The following response is the Code Clode Function to delete a user:
Parse User Destroy - Javascript in Cloud Code
I haven't tried doing this, but I have these ideas:
Make sure your ACL allows this to the public
Is it not acceptable to call a cloud code function? This would feel more intuitive to me than using the API in this case. Can you explain why you must use the REST API?
If that doesn't work, look at this answer: https://www.parse.com/questions/delete-a-user-rest-api
Basically you might have to pass the Master Key header, which is less than ideal to expose to the public. Is it not accep

Why does my FB.api request always produce an error response?

I have a Facebook app with namespace 'thenovel'. It has an action 'rate' assigned to an object 'sentence'. When the 'dofacebookstuff()' function is called, I want the user's rating of a sentence - 'testsentence', for the moment - to be published to their timeline. 'Rating' doesn't require an access token.
Whenever the dofacebookstuff() is called, the error alert pops up. I have tested whether the '!response' or 'response.error' was causing it, and it is the 'response.error' every time.
I can get the user id, name etc. fine with FB.login but the api call just does not work. If I go straight to 'https://graph.facebook.com/me/thenovel:rate?sentence=testsentence&access_token=XXXXX', I see the following:
{
"data": [ ],
"paging": {
"next": "https://graph.facebook.com/me/thenovel:rate?sentence=testsentence&access_token=XXXXX&offset=25&limit=25"
}
}
I have checked my HTML and all the Facebook-related stuff is there and in the right place. None of the other scripts on the page produce errors. Here is the function in question:
function dofacebookstuff() {
FB.api(
'/me/thenovel:rate?sentence=hello&access_token=' + accesstoken,
'post',
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Action ID: ' + response.id);
}
});
}
Could anyone explain where I'm going wrong? Where is my response data?
If you're posting to /me you need an active access token for that user - do you have one? it may have expired already, but i think the message is specifically referring to the fact that you haven't provided one, as there'd be a specific message if the token is invalid or expired.
You should be able to post to /USER_ID/thenovel:rate with the app access token if you're doing offline publishing of some kind
Try logging in using FB connect first.

Categories