gpt4 book ai didi

c# - 通过 Facebook 聊天 API (XMPP) C# 发送消息

转载 作者:IT王子 更新时间:2023-10-29 03:56:37 24 4
gpt4 key购买 nike

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

The service and API this document covers has been deprecated with the release of Platform API v2.0. Once version 1.0 is deprecated, chat.facebook.com will no longer be available.

Important! Read this and you probably want to do something completely different than anything that has to do with this question.

我正在使用连接到 Facebook 聊天 API 的 WebForms C# 创建聊天。

我也看过this SO question (和所有链接)。有些部分不再相关,因为 Facebook 现在需要 auth_token

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

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

  1. 文档中标记为 //finishes auth process 的过程与 documentation description 不匹配(在收到来自 Facebook 的基于 SSL/TLS 的成功消息后,我没有收到任何回复。)
  2. 我不知道“发送聊天消息”部分应该如何设置,而且由于我没有收到来自 Facebook 的任何消息,所以很难判断哪里出了问题。

Here is my code in its entirety, on 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 将我的身份验证 token 和用户名设置为全局变量。 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'/>

最后我执行了 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;
}

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

最佳答案

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

使用 Messenger 平台不需要支付任何费用。它旨在让企业处理客户的查询。一旦您为客户建立了互动体验,您就可以利用广告将人们带到您的体验中,例如点击信使广告或收件箱广告。

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

在这里与示例业务聊天

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

为了开始,您需要 Messenger、一个 Facebook 页面和一个要将 webhook 发送到的 url。

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

准备好 build 了吗?开始吧

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

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

关于c# - 通过 Facebook 聊天 API (XMPP) C# 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19590752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com