Ejabberd - 将消息路由给 offline_message_hook 中的用户

Ejabberd - Route message to users in offline_message_hook

我想构建一个客户支持聊天应用程序。有 users 和一个 admin。 admin 下面有multiple sub-admins。最初只与管理员发起聊天,但如果管理员离线,我需要将消息路由给子管理员。

offline_message_hook 挂钩就达到了目的。我会检查 To 是否是管理员,然后我需要将 Packet 路由到其中一个子管理员。我如何 route/send 将数据包发送给 offline_message_hook 内的其他用户。简而言之,如何更改数据包中的 To,以便将数据包重定向到新的子管理员?

这是我试过的:-

offline_message_hook({_Action, #message{from = Peer, to = To} = Pkt} = Acc) ->
    ?INFO_MSG("Inside offline", []),
    ejabberd_router:route(From, To, Packet),
    ok.

我正在使用 ejabberd 17.04.105

更新

听从 user2610053 的建议后,我这样做了:-

-spec offline_message_hook({any(), message()}) -> {any(), message()}.
offline_message_hook({_Action, Msg} = Acc) ->
    ejabberd_router:route(xmpp:set_to(Msg, 'praful@localhost')),
    {routed, Msg}.

错误如下:-

15:13:12.291 [error] failed to route packet:
#message{id = <<"purple187f6502">>,type = chat,lang = <<"en">>,
         from = {jid,<<"praful2">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>,
                     <<"praful2">>,<<"localhost">>,<<"Prafuls-MacBook-Pro">>},
         to = praful@localhost,subject = [],
         body = [#text{lang = <<>>,data = <<"co=umon">>}],
         thread = undefined,
         sub_els = [{xmlel,<<"active">>,
                           [{<<"xmlns">>,
                             <<"http://jabber.org/protocol/chatstates">>}],
                           []}],
         meta = #{ip => {0,0,0,0,0,0,0,1}}}
Reason = {error,{{badrecord,jid},[{ejabberd_router,do_route,1,[{file,"src/ejabberd_router.erl"},{line,343}]},{ejabberd_router,route,1,[{file,"src/ejabberd_router.erl"},{line,87}]},{mod_sunshine,offline_message_hook,1,[{file,"src/mod_sunshine.erl"},{line,24}]},{ejabberd_hooks,safe_apply,4,[{file,"src/ejabberd_hooks.erl"},{line,380}]},{ejabberd_hooks,run_fold1,4,[{file,"src/ejabberd_hooks.erl"},{line,364}]},{ejabberd_sm,route,1,[{file,"src/ejabberd_sm.erl"},{line,138}]},{ejabberd_local,route,1,[{file,"src/ejabberd_local.erl"},{line,116}]},{ejabberd_router,do_route,1,[{file,"src/ejabberd_router.erl"},{line,348}]}]}}

用户praful@localhost存在。请指教究竟是哪里出了问题?

Update2 - `UserReceivePacket 钩子

user_receive_packet 数据包挂钩中,在使用相同的函数 ejabberd_router:route(xmpp:set_to(Packet, jid:decode("praful@localhost"))), 时会抛出一条错误消息:-

Hook user_receive_packet crashed when running mod_sunshine:user_receive_packet/1:
** Reason = {error,function_clause,[{jid,decode,[{file,"src/jid.erl"},{line,132}],["praful@localhost"]},{mod_sunshine,user_receive_packet,[{file,"src/mod_sunshine.erl"},{line,29}],1},{ejabberd_hooks,safe_apply,[{file,"src/ejabberd_hooks.erl"},{line,380}],4},{ejabberd_hooks,run_fold1,[{file,"src/ejabberd_hooks.erl"},{line,364}],4},{ejabberd_c2s,process_info,[{file,"src/ejabberd_c2s.erl"},{line,231}],2},{ejabberd_hooks,safe_apply,[{file,"src/ejabberd_hooks.erl"},{line,380}],4},{ejabberd_hooks,run_fold1,[{file,"src/ejabberd_hooks.erl"},{line,364}],4},{xmpp_stream_in,handle_info,[{file,"src/xmpp_stream_in.erl"},{line,373}],2}]}

因此,我阅读了有关 function_clause 的内容,但无法理解。这里到底出了什么问题?

我想你问的是 xmpp:set_to/2。这是一个例子:

offline_message_hook({_Action, Msg} = Acc) ->
  SubAdmins = get_sub_admins(Msg#message.to),
  lists:foreach(
    fun(Admin) ->
      ejabberd_router:route(xmpp:set_to(Msg, Admin))
    end, Admins),
  {routed, Msg}.