创建自定义IQ模块

时间:2018-05-30 13:34:22

标签: erlang ejabberd iq

我有样本模块,

-module(mod_iq_test).
-behaviour(gen_mod).
-export([start/2,
         init/2,
         stop/1,
         process_sm_iq/3
    ]).

-define(PROCNAME, ?MODULE).

-include("ejabberd.hrl").
-include("jlib.hrl").
-include("logger.hrl").

-define(NS_TEST, <<"http://jabber.org/protocol/test">>).

start(Host, Opts) ->
    ?INFO_MSG("Loading module 'mod_iqtest' v.01", []),
    ok.

init(Host, _Opts) ->
    IQDisc = gen_mod:get_opt(iqdisc, _Opts, fun(A) -> A end, one_queue),
    gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_TEST, ?MODULE, process_sm_iq, IQDisc),
    ?INFO_MSG("added iq handlers 'mod_iqtest' v.01", []),
    ok.

stop(Host) ->
    gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_TEST),
    ?INFO_MSG("Stoping module 'mod_iqtest' ", []),
    ok.

process_sm_iq(_From, _To, IQ) ->
    ?INFO_MSG("Processing IQ Get query:~n ~p", [IQ]),
    IQ#iq{type = result, sub_el = [{xmlel, <<"value">>, [], [{xmlcdata, <<"Hello World of Testing.">>}]}]}.

发送智商时,

<iq type='get'>
  <query xmlns='http://jabber.org/protocol/test'/>
</iq>
<r xmlns="urn:xmpp:sm:3"/>

它会抛出错误,

<iq type="error">
  <query xmlns="http://jabber.org/protocol/test"/>
<error code="400" type="modify">
<bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
<text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="en">Missing attribute 'id' in tag &lt;iq/&gt; qualified by namespace 'jabber:client'</text>
</error>
</iq>

1 个答案:

答案 0 :(得分:0)

尝试提供id属性,如错误响应中所述:

<iq type='get' id='aaa8821'>
  <query xmlns='http://jabber.org/protocol/test'/>
</iq>