发送 XMPP 消息而不开始聊天
我基本上正在编写一个 XMPP 客户端来自动回复“特定”聊天消息。
我的设置是这样的:
这是我的要求:
我正在尝试自动化在 gtalk 上收到的某些类型的消息。因此,每当我收到特定消息(例如:“你好吗”)时,我自己的 XMPP 客户端应该自动回复说“很好”。你好吗”。(在我的客户回复之前和之后)发送到 [电子邮件受保护],但应由所有客户端接收(我自己的客户端没有 UI,只能响应特定消息。)。
现在我已经对我的客户进行了编码以自动回复。这很好用。但我面临的问题是,一旦我回复(我使用 smack 库),所有后续消息都会发送到 [电子邮件受保护] 仅由我的 XMPP 客户端接收。这显然是一个问题,因为我自己的客户端非常垃圾并且没有 UI,所以我看不到发送给我的其余消息,从而使我“丢失”消息。
我也观察到其他 XMPP 客户端有相同的行为。现在的问题是,这是 XMPP 的要求吗(很抱歉,我没有很好地阅读 XMPP 协议)。是否可以对 XMPP 客户端进行编码以向用户发送回复,并且仍然能够在当前侦听消息的所有客户端中接收所有后续消息?让我的客户端成为成熟的 XMPP 客户端是一个解决方案,但我不想走这条路。
我希望我的问题很清楚。
I am basically writing a XMPP client to automatically reply to "specific" chat messages.
My setup is like this:
Here is my requirement:
I am trying to automate certain kind of messages that I receive on gtalk. So whenever I receive a specific message eg: "How are you" , my own XMPP client should reply automatically with say "fine". How are you". All messages sent (before and after my client replies) to [email protected] but should be received by all clients (my own client does not have a UI and can only respond to specific messages.).
Now I have already coded my client to reply automatically. This works fine. But the problem I am facing is that as soon as I reply (I use the smack library), all subsequent messages that are sent to [email protected] are received only by my XMPP client. This is obviously a problem as my own client is quite dump and does not have a UI, so I don't get to see the rest of the messages sent to me, thereby making me "lose" messages.
I have observed the same behavior with other XMPP clients as well. Now the question is, is this is a requirement of XMPP (I am sorry but I haven't read XMPP protocol too well). Is it possible to code an XMPP client to send a reply to a user and still be able to receive all subsequent messages in all clients currently listening for messages? Making my client a full fledged XMPP client is a solution, but I don't want to go that route.
I hope my question is clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能必须为您的机器人设置负的存在优先级。
首先要知道的是,在 XMPP 协议中,每个客户端都应该有一个完整的 JID。这是一个裸 JID - 在您的情况下 [email protected] 带有资源最后例如 [email protected]/pidgin 或 [email protected]/home(其中 /pidgin 和 /home 是资源)。这是如何将消息路由到不同客户端的一部分。
然后是存在节。当上线时,客户端通常会向服务器发送存在节。例如,这可以告知客户是否可以聊天或外出吃午餐。可以与此信息一起发送优先级。当有多个客户端连接时,优先级最高的客户端将接收发送到裸 JID 的消息(例如 ClientA(prio=50) 和 ClientB(prio=60) -> ClientB 接收发送到 [电子邮件受保护])。但也有一些负面的优先事项。优先级小于 0 表示永远不应向该客户端发送任何消息。这样的节可能看起来像这样
这可能适合您的情况。请记住,它还取决于您的帐户所在的 XMPP 服务器,该服务器可能会或可能尚未完全实现协议的这一部分。
总结一下:我建议您查看 Smack API 如何设置存在状态,并在连接后立即将机器人客户端的优先级设置为 <0。
You may have to set a negative presence priority for your bot..
First thing to know is that in XMPP protocol every client is supposed to have a full JID. This is a bare JID - in your case [email protected] with a resource in the end e.g. [email protected]/pidgin or [email protected]/home (where /pidgin and /home are the resource). This is a part of how routing messages to different clients is supposed to be achieved.
Then there are the presence stanzas. When going online a client usually sends a presence stanza to the server. This informs about e.g. if the client is available for chat or away for lunch. Along with this information can be sent a priority. When there are more than one clients connected the one with the highest priority will receive the messages sent to the bare JID (e.g. ClientA(prio=50) and ClientB(prio=60) -> ClientB receives the messages sent to [email protected]). But there are also negative priorities. A priority less than 0 states that this client should never be sent any messages. Such a stanza might look like this
This may fit your case. Please keep in mind it also depends on the XMPP server where your account is located, which may or may have not fully implemented this part of the protocol.
So to summarize: I recommend you to look through the Smack API how to set a presence and set the priority to <0 for your bot client right after it connected.