0

目前还没有创建 MIX 频道的 api。我为此编写了一个自定义模块。

到目前为止,我已经编写了以下代码。但我不知道如何进一步进行。我真的很感谢有人在这里指导。提前致谢。

-module(mod_custom).
-behaviour(gen_mod).
-include("logger.hrl").
-export([start/2, stop/1, reload/3, mod_options/1,
     get_commands_spec/0, depends/2]).
-export([
     % Create channel
     add_channel/4
    ]).

-include("ejabberd_commands.hrl").
-include("ejabberd_sm.hrl").
-include("xmpp.hrl").

start(_Host, _Opts) ->
    ejabberd_commands:register_commands(get_commands_spec()).
stop(Host) ->
    case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
    false ->
        ejabberd_commands:unregister_commands(get_commands_spec());
    true ->
        ok
    end.
reload(_Host, _NewOpts, _OldOpts) ->
    ok.
depends(_Host, _Opts) ->
    [].

get_commands_spec() ->
    [
        #ejabberd_commands{name = add_channel, tags = [group],
            desc = "Create a WhatsApp like group",
            module = ?MODULE, function = add_channel,
            args = [{jid, binary}, {channel, binary}, {id, binary}],
            args_example = [<<"admin@localhost">>, <<"testgroup123@localhost">>, <<"abc123456">>],
            args_desc = ["Admin JID", "Channel JID", "Unique ID"],
            result = {res, rescode}}
    ].

add_channel(JID, Channel, ID) ->
    %%% Create channel code goes here...
ok.

mod_options(_) -> [].
4

1 回答 1

1

尝试这样的事情:

-module(mod_custom).
-behaviour(gen_mod).

-export([start/2, stop/1, reload/3, mod_options/1,
         get_commands_spec/0, depends/2]).
-export([create_channel/3]).

-include("logger.hrl").
-include("ejabberd_commands.hrl").
-include("ejabberd_sm.hrl").
-include_lib("xmpp/include/xmpp.hrl").

start(_Host, _Opts) ->
    ejabberd_commands:register_commands(get_commands_spec()).
stop(Host) ->
    case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
        false ->
            ejabberd_commands:unregister_commands(get_commands_spec());
        true ->
            ok
    end.
reload(_Host, _NewOpts, _OldOpts) ->
    ok.
depends(_Host, _Opts) ->
    [].

get_commands_spec() ->
    [#ejabberd_commands{name = create_channel, tags = [group],
                        desc = "Create a WhatsApp like group",
                        module = ?MODULE, function = create_channel,
                        args = [{from, binary},
                                {channel, binary},
                                {service, binary}],
                        args_example = [<<"admin@localhost">>,
                                        <<"testgroup123">>,
                                        <<"mix.localhost">>],
                        args_desc = ["From JID", "Channel Name", "MIX Service"],
                        result = {res, rescode}}
    ].

create_channel(From, ChannelName, Service) ->
    try xmpp:decode(
          #xmlel{name = <<"iq">>,
                 attrs = [{<<"to">>, Service},
                          {<<"from">>, From},
                          {<<"type">>, <<"set">>},
                          {<<"id">>, p1_rand:get_string()}],
                 children =
                     [#xmlel{name = <<"create">>,
                             attrs = [{<<"channel">>, ChannelName},
                                      {<<"xmlns">>, ?NS_MIX_CORE_0}]}
                     ]},
          ?NS_CLIENT, []) of
        #iq{type = set} = Iq ->
            case mod_mix:process_mix_core(Iq) of
                #iq{type = result} ->
                    ok;
                _ ->
                    {error, unexpected_response}
            end
    catch _:{xmpp_codec, Why} ->
            {error, xmpp:format_error(Why)}
    end.

mod_options(_) -> [].
于 2020-10-26T10:41:34.060 回答