【问题标题】:Send message from ejabberd using Erlang使用 Erlang 从 ejabberd 发送消息
【发布时间】:2015-07-01 04:07:16
【问题描述】:

我有简单的 ejabebrd 插件,我需要从中发送消息。在这里我发布了我的代码示例和我得到的错误。请给我你的反馈意见。

我的代码:

-module(mod_final).
-on_load(send_message/0).
-behaviour(gen_mod).
-export([start/2, stop/1]).

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

start(_Host, _Opt) ->
        ?INFO_MSG("mod_final starting", []),
        ok.

stop(_Host) ->
    ?INFO_MSG("mod_final stoping", []),
        ok.

send_message() ->
    LUser = "test1",
    LServer = "localhost",
    From = jlib:make_jid(LUser, LServer, []),

    TUser = "test2",
    TServer = "localhost",
    To = jlib:make_jid(TUser, TServer, []),

    FromAddress = jlib:jid_to_string(From),
    ToAddress = jlib:jid_to_string(To),

    ?INFO_MSG("send message starting", []),

    XmlBody = {xmlelement, "message", [{"id", []},{"type", "chat"}, {"from", FromAddress}, {"to", ToAddress}], [{xmlelement, "body", [], [{xmlcdata, <<"Test Message">>}]}]},

   ejabberd_router:route(From, To, XmlBody).

但我收到了这个错误:

   D(<0.252.0>:ejabberd_router:313) : route
    from {jid,"test1","localhost",[],"test1",
                  "localhost",[]}
    to {jid,"test2","localhost",[],"test2",
                "localhost",[]}
    packet {xmlelement,"message",
                   [{"type","error"},
                    {"to","test2@localhost"},
                    {"from","test1@localhost"},
                    {"id",[]}],
                   [{xmlelement,"body",[],[{xmlcdata,<<"Test Message">>}]},
                    {xmlelement,"error",
                        [{"code","503"},{"type","cancel"}],
                        [{xmlelement,"service-unavailable",
                             [{"xmlns","urn:ietf:params:xml:ns:xmpp-stanzas"}],
                             []}]}]}

感谢您的帮助。

【问题讨论】:

    标签: erlang xmpp ejabberd


    【解决方案1】:

    我的建议是不要创建自己的 XML。您可以使用此功能捕获数据包,然后写入错误日志

    on_filter_packet({From, To, XML} = Packet) ->
        %% does something with a packet
        %% should return modified Packet or atom `drop` to drop the packet
    
        error_logger:info_msg(XML, [a_module]),
    
        %% or use error_logger:info_msg(xml:get_subtag(XML, <<"body">>), [a_module]),
        %% to take a body
        Packet.
    

    那么你可以使用相同的xml,但是改变xmlcdata

    【讨论】:

      猜你喜欢
      • 2011-10-17
      • 1970-01-01
      • 2022-06-29
      • 2014-06-07
      • 2019-07-23
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      相关资源
      最近更新 更多