简体   繁体   English

如何使用Facebook C#SDK阅读/发送私人消息?

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

I am starting with the Facebook C# SDK. 我从Facebook C#SDK开始。 How do I deal with messages in the official SDK documentation? 如何处理官方SDK文档中的消息? How do I read and send messages? 如何阅读和发送消息? Is there any tutorial? 有教程吗?

To get access to messages (the chat ones) u have to have the read_mailbox extended permission (i think) and do like: 要访问消息(聊天的消息),您必须具有read_mailbox扩展权限(我认为),并执行以下操作:

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 您可以尝试在https://developers.facebook.com/tools/explorer

Read more in: 阅读更多内容:

Inbox Notifications , Message Info 收件箱通知邮件信息

Changes: coming soon changes 变化: 即将到来的变化

Hope it helped ;) 希望它有所帮助;)

Facebook不允许SDK发送私人消息,以防止垃圾邮件发送者滥用

this is how to display inbox using c#,asp.net: 这是使用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();

}

This is for sending messages .. 这是用于发送消息..

Using the Facebook C# SDK (http://facebooksdk.codeplex.com) 使用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. 您也可以使用该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. Facebook不允许应用程序直接向用户发送消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM