如何在ejabberd的xmpp消息正文中附加自定义数据 [英] How do I append custom data in the body of a xmpp message in ejabberd

查看:147
本文介绍了如何在ejabberd的xmpp消息正文中附加自定义数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Pidgin和Ejabberd建立了一个聊天settong。我使用 user_send_packet 在ejabberd中写下了一个自定义模块:

I have set up a chat settong using Pidgin and Ejabberd.I have written down a custom module in ejabberd using user_send_packet:

ejabberd_hooks:add(user_send_packet, _Host, ?MODULE,
           myMessage, 95),

函数myMessage如下:

The function myMessage is as follows:

myMessage({Packet, C2SState})->


PacketType=xmpp:get_name(Packet),
case PacketType of
<<"iq">>->
ok;
<<"presence">>->
ok;
<<"message">>->

Sum=2+2,
?INFO_MSG("Sum is ~p~n",[Sum])

end,

{Packet,C2SState}.

此功能的基本作用是,每当有人发送聊天消息时说你好,那里 总和的值被计算并打印在服务器及其日志上,并且消息 hello there被发送给第二个用户。

Basically what this function does is that whenever someone sends a chat message say "hello there", the value of Sum gets calculated and printed on the server and its logs and the message ""hello there" is sent to the second user.

但是现在我想将 Sum 的值以及消息 hello there 发送到第二个用户,例如:

But Now I want send the value of Sum along with the message "hello there" to the second user for example:

"hello there Sum is 4" 

有人可以帮我吗?

预先感谢。

推荐答案

在这里是

process_message({#message{body = Body} = Msg, C2SState})->
    Sum = calc_sum_and_return_as_binary(),
    NewBody = lists:map(
        fun(#text{data = Data} = Txt) ->
            Txt#text{data = <<Data/binary, Sum/binary>>}
        end, Body),
    {Msg#message{body = NewBody}, C2SState};
process_message(Acc) ->
    Acc.

请注意,该 #text {} 记录包含 lang 字段,如果您要支持要附加的文本的国际化,则可以使用该字段。

Note, that #text{} record contains lang field which can be used if you want to support internationalization of the text being appended.

这篇关于如何在ejabberd的xmpp消息正文中附加自定义数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆