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

限于喜欢 提交于 2019-12-11 05:49:07

问题


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?


回答1:


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




回答2:


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 ;)




回答3:


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();

}



回答4:


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.



来源:https://stackoverflow.com/questions/8016939/how-to-read-send-private-messages-with-the-facebook-c-sharp-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!