How do I append custom data in the body of a xmpp message in ejabberd

偶尔善良 提交于 2019-12-11 17:56:11

问题


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),

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

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.

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" 

Can anyone help me out with this?

Thanks in advance.


回答1:


Here it is:

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.

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



来源:https://stackoverflow.com/questions/45455387/how-do-i-append-custom-data-in-the-body-of-a-xmpp-message-in-ejabberd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!