通过Facebook Chat API(XMPP)C#发送消息

Mag*_*son 49 c# facebook xmpp facebook-chat

////////////////////////////////////////////////// ////////////////////////////////////////////////

// OBSERVE https://developers.facebook.com/docs/chat/

随着Platform API v2.0的发布,本文档所涵盖的服务和API已被弃用.不推荐使用1.0版后,chat.facebook.com将不再可用.

//阅读本文,你可能想做一些与这个问题有关的事情完全不同的事情.

////////////////////////////////////////////////// //////////////////////////////////////

我正在创建一个与Web Chatms C#连接到Facebook Chat API的聊天.

我也看了这个问题(以及所有链接).由于Facebook auth_token现在需要,因此某些部分不再相关.

要复制此功能,您应该设置一个Facebook Web应用程序,使用appId具有xmpp_login权限集的用户帐户.然后Chat.aspx在后面创建一个代码并相应地粘贴此代码.并替换硬编码用户进行交互.

我有两个(可能是三个)问题我相信会阻止我成功发送聊天消息.

  1. // finishes auth process文档中注明的过程与文档描述不符 (在我从Facebook收到基于SSL/TLS的成功消息后,我没有得到任何响应.)
  2. 我不知道应该如何设置"发送聊天消息"部分,因为我没有从Facebook收到任何消息,很难说出可能出现的问题.

以下是我在PasteBin上的完整代码.

我还有一些帮助,用于添加xmpp_login权限等等..为了清楚起见,删除了.

全局变量:

public partial class Chat : Page
{
    public TcpClient client = new TcpClient();
    NetworkStream stream;
    private SslStream ssl;
    private string AppId { get; set; }
    public string AppSecret { get; set; }
    public string AppUrl { get; set; }
    public string UserId { get; set; }
    public string AccessToken { get; set; }
    private string _error = string.Empty;//global error string for watch debugging in VS. 

    public const string FbServer = "chat.facebook.com";
    private const string STREAM_XML = "<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" xmlns=\"jabber:client\" to=\"chat.facebook.com\" xml:lang=\"en\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">";
    private const string AUTH_XML = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='X-FACEBOOK-PLATFORM'></auth>";
    private const string CLOSE_XML = "</stream:stream>";
    private const string RESOURCE_XML = "<iq type=\"set\" id=\"3\"><bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"><resource>fb_xmpp_script</resource></bind></iq>";
    private const string SESSION_XML = "<iq type=\"set\" id=\"4\" to=\"chat.facebook.com\"><session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/></iq>";
    private const string START_TLS = "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>";
Run Code Online (Sandbox Code Playgroud)

然后在Page_Load所有需要的步骤中执行(或应该执行).值得注意的是SendMessage("test");.我只是试着把它放在那里看看它是否会成功发送聊天消息...... SetUserNameAndAuthToken将我的身份验证令牌和用户名设置为全局变量.AuthToken有效.

protected void Page_Load(object sender, EventArgs e)
{
    this.AppId = "000000082000090";//TODO get from appsettings.
    //AddAdditionalPermissions("xmpp_login");//TODO handle xmpp_login persmission
    this.AppSecret = "d370c1bfec9be6d9accbdf0117f2c495"; //TODO Get appsecret from appsetting.
    this.AppUrl = "https://fbd.anteckna.nu";

    SetUserNameAndAuthToken();

    Connect(FbServer);

    // initiates auth process (using X-FACEBOOK_PLATFORM)
    InitiateAuthProcess(STREAM_XML);

    // starting tls - MANDATORY TO USE OAUTH TOKEN!!!!
    StartTlsConnection(START_TLS);

    // gets decoded challenge from server
    var decoded = GetDecodedChallenge(AUTH_XML);

    // creates the response and signature
    string response = CreateResponse(decoded);

    //send response to server
    SendResponseToServer(response);

    SendMessage("test");

    // finishes auth process
    FinishAuthProcess();

    // we made it!
    string streamresponseEnd = SendWihSsl(CLOSE_XML);

}
Run Code Online (Sandbox Code Playgroud)

所以我得到一个响应,然后我将响应发送到服务器:

private void SendResponseToServer(string response)
{
    string xml = String.Format("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">{0}</response>", response);
    string response2 = SendWihSsl2(xml);
    if (!response2.ToLower().Contains("success"))
        _error = response2;
}
Run Code Online (Sandbox Code Playgroud)

这需要1分40秒......响应是:

<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
Run Code Online (Sandbox Code Playgroud)

最后我做了FinishAuthPorcess()

private void FinishAuthProcess()
{
    string streamresponse = SendWithSsl(STREAM_XML);
    if (!streamresponse.Contains("STREAM:STREAM"))
        _error = streamresponse;

    string streamresponse2 = SendWihSsl(RESOURCE_XML);
    if (!streamresponse2.Contains("JID"))
        _error = streamresponse2;

    string streamresponse3 = SendWihSsl(SESSION_XML);
    if (!streamresponse3.Contains("SESSION"))
        _error = streamresponse2;
}
Run Code Online (Sandbox Code Playgroud)

所有回复都是"".看一下Read方法SendWithSsl:它是0字节.尝试发送消息也给我0字节从Facebook读取数据.我不知道为什么?

Jin*_*kur 1

现在有新的API了。Messenger 平台如何运作?当某人在 Messenger 上向企业发送消息时,只要此主页使用应用程序部分或完全自动化对话,就会发生以下情况。Facebook 服务器将 Webhook 发送到托管消息应用程序的业务服务器的 URL。使用发送 API,应用程序可以回复 Messenger 上的人员。通过这种方式,开发人员可以构建引导式对话,引导人们完成自动化流程,或者构建一个应用程序,作为您的代理与您在 Messenger 上的业务存在之间的桥梁。

Messenger 平台不需要任何付款即可使用。它旨在供企业处理客户的询问。一旦您构建了可供客户互动的体验,您就可以利用广告将人们带入您的体验,例如点击信使广告或收件箱广告。

Messenger for Business 体验是什么样的?我们有一个名为 Original Coast Clothing 的示例电子商务业务,您可以在 Messenger 上与之聊天。

在这里与示例业务聊天

我该如何开始?您可以通过创建一个测试页面和测试应用程序来开始尝试 Messenger 中的体验。我们有一些示例可以帮助您入门。一旦您的应用程序准备好部署,您就可以提交它以供审核。当您的应用程序通过我们的审核流程后,它就可以与公众互动了。

首先,您需要 Messenger、Facebook 页面以及要发送 Webhook 的 URL。

这是将上述体验部署到测试页面中的分步指南,以帮助您入门。

准备好构建了吗?开始使用

https://developers.facebook.com/products/messenger/

https://developers.facebook.com/docs/messenger-platform/reference/send-api/