Dhia Elhak Louhichi
Dhia Elhak Louhichi

Reputation: 83

How to read/send private messages with the Facebook C# SDK?

I am starting with the Facebook C# SDK. How do I deal with messages in the official SDK documentation? How do I read and send messages? Is there any tutorial?

Upvotes: 1

Views: 18053

Answers (4)

Ifwat
Ifwat

Reputation: 1553

this is how to display inbox using c#,asp.net:

protected void Button4_Click(object sender, EventArgs e)
{
    var fb = new FacebookClient(lblToken.Text);

    var query = string.Format(@"SELECT message_id, author_id, body, created_time FROM message WHERE thread_id IN (SELECT thread_id FROM thread WHERE folder_id = 0)");

    dynamic parameters = new ExpandoObject();
    parameters.q = query;
    dynamic results = fb.Get("/fql", parameters);

    List<MyMessage> q = JsonConvert.DeserializeObject<List<MyMessage>>(results.data.ToString());

    GridView4.DataSource = q;
    GridView4.DataBind();

}

Upvotes: 1

Mr Shadi
Mr Shadi

Reputation: 36

Facebook does not allow the SDK to send private messages to prevent misuse by spammers

Upvotes: 1

S&#237;lvio N.
S&#237;lvio N.

Reputation: 361

To get access to messages (the chat ones) u have to have the read_mailbox extended permission (i think) and do like:

string facebookToken = "your facebook token here";
var client = new FacebookClient(facebookToken);

dynamic result = client.Get("me/inbox", null);

foreach (dynamic item in result.inbox.data)
{
    //item is a conversation
    //the latest updated conversations come first so
    //im just gona grab the first conversation with unreaded / unseen messages

    if (item.unread > 0 || item.unseen > 0)
    {
        string conversationID = item.id;
        string otherPerson = item.to.data[1].name;//the item.to.data[0] its myself

        //you can access the messages of the conversation like
        //by default it will return the last 25 messages, u can get more, by making a call too
        //"https://graph.facebook.com/{0}/comments?limit={1}" like:
        //dynamic result = client.Get(string.Format("{0}/comments?limit={1}",conversationID, 100), null);
        foreach (dynamic message in item.comments.data)
        {
            //Do want you want with the messages
            string id = message.id;
            string fromName = message.from.name;
            string fromID = message.from.id;
            string text = message.message;
            string createdDate = message.created_time;
        }

        //To send a message in this conversation, just
        dynamic parameters = new ExpandoObject();
        parameters.message = "A message from code!";
        client.Post(string.Format("{0}/comments", conversationID), parameters);
        //or
        //client.Post(string.Format("{0}/comments", conversationID), new Dictionary<string, object> { { "message", "A message from code!" } });

        //NOTE!! - The application must be on white list for you te be able to post a message 
        // read - https://developers.facebook.com/docs/ApplicationSecurity/

        break;
    }
}

You can try at https://developers.facebook.com/tools/explorer

Read more in:

Inbox Notifications , Message Info

Changes: coming soon changes

Hope it helped ;)

Upvotes: 5

Glory Raj
Glory Raj

Reputation: 17691

This is for sending messages ..

Using the Facebook C# SDK (http://facebooksdk.codeplex.com)

var app = new FacebookApp("access_token");
var parameters = new Dictionary<string, object>();
parameters["message"] = "This is a test message";
app.Api("/me", parameters, HttpMethod.Post);

That will post a message to the current user's wall. You can also post images using that SDK. There are samples in the tests on how to do that. Note, if you meant that you wanted to sent them a private message rather than post on their wall that is not possible. Facebook does not allow applications to send messages directly to users.

Upvotes: 0

Related Questions