我正在使用名为" filter-packet"的ejabberd hook。制作一个模块。在这里,我想向数据包添加一个元素。怎么做?我的代码是 -
on_filter_packet({From, To, Packet}=Input) ->
Type = xml:get_tag_attr_s(list_to_binary("type"), Packet),
if (Type == <<"groupchat">>) ->
?INFO_MSG("type is group chat", []),
NPacket={Packet, [{xmlelement, "time",
[],
[{xmlcdata, "testtime"}]}]},
{From, To, NPacket};
true ->
Input
end.
此代码表示错误匹配错误。任何帮助?
答案 0 :(得分:2)
13.12对xmlelement使用不同的类型。
Packet是一种记录#xmlel,因此您需要向Packet#xmlel.children插入新元素。
on_filter_packet({From, To, #xmlel{ children=OldChildren } = Packet}=Input) ->
...
TimeElem = #xmlel{ name = <<"time">>,
children =
[{xmlcdata, <<"testtime">>}]},
NPacket = Packet#xmlel{ children = [TimeElem|OldChildren] },
...
未经测试,但可以正常使用。