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

观察 https://developers.facebook.com/docs/chat/

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

重要!阅读这篇文章,你可能想做一些与这个问题完全不同的事情。

我正在用 WebForms C # 创建一个连接到 Facebook Chat API 的聊天功能。

我也看了 这个所以问题(和所有的链接)。有些部分不再相关,因为 Facebook 现在需要 auth_token

要复制这一点,您应该设置一个 Facebook web 应用程序,使用 appId和一个设置了 xmpp _ login 权限的用户帐户。然后创建一个带有代码的 Chat.aspx,并相应地粘贴这些代码。并替换硬编码的用户进行交互。

我有两个(也许是三个)问题,我认为这些问题阻碍了我实现发送聊天消息的目标。

  1. 文档中标记为 // finishes auth process的过程与 < a href = “ https://developers.facebook.com/docs/chat/”rel = “ noReferrer”> 文档描述不匹配 (在我收到来自 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\"/>";

然后在 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);


}

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

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

这需要1分40秒,回答是:

<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>

最后执行 FinishAuthProcess ()

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

所有的响应都是 ""。看看 SendWithSsl中的 Read方法: 它是0字节。 尝试发送消息也给我0字节从 Facebook 读取数据。我不知道为什么?

10133 次浏览

There is new api now. How does the Messenger Platform work? When a person sends a message to a business on Messenger and as long as this Page uses an app to partially or fully automate conversations, the following will happen. The Facebook server sends webhooks to the URL of the business server, where the messaging app is hosted. Using the Send API, the app can respond to the person on Messenger. In this way, developers can build guided conversations to lead people through an automated flow or build an app to serve as a bridge between your agents and your business presence on Messenger.

The Messenger Platform does not require any payment to use. It is meant for businesses to handle inquiries from their customers. Once you build your experience for your customers to interact with, you can then leverage Ads to bring people to your experience, like for example Click-to-Messenger Ads or Inbox Ads.

What does a Messenger for Business experience look like? We have a sample eCommerce business that you can chat with on Messenger called Original Coast Clothing.

Chat with Sample Business Here

How do I get started? You can get started by creating a test page and test app that allows you to try your experience within Messenger. We have some examples that can get you started. Once your app is ready to deploy, you can submit it for review. When your app passes our review process, it will be ready to interact with the public.

In order to get started, you will need Messenger, a Facebook Page, and a url where the webhooks to be sent to.

Here is a step-by-step guide to deploy the above experience into your test page to get you started.

Ready to Build? Get Started

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

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