Search code examples
erlangmongoose-im

How to run queries from MongooseIM module


I am trying to make MongooseIM module, that will trigger with offline_message_hook, count number of pending messages in offline storage for the user and send it to an URL through GET method. Below is my code.

send_notice(From, To, Packet) ->
    Type = xml:get_tag_attr_s(list_to_binary("type"), Packet),
    Body = xml:get_path_s(Packet, [{elem, list_to_binary("body")}, cdata]),
    PostUrl = "http://myurl.com/",
    Count = count_msg(To),
    GetParam = "?count=",
    FullUrl = PostUrl ++ GetParam ++ Count,

    ?DEBUG("POST URL : ~s",[FullUrl]),

    if (Type == <<"chat">>) and (Body /= <<"">>) ->
              Sep = "&",
        Post = [
          "alert=", url_encode(binary_to_list(Body)), Sep,
                                        "badge=", url_encode("+1"), Sep,
          % "sound=", Sound, Sep,
          "channel=", To#jid.luser, Sep,
          "info[from]=", From#jid.luser, Sep],
          % "auth_token=", Token],

        ?INFO_MSG("Sending post request to ~s with body \"~s\"", [FullUrl, Post]),

        httpc:request(post, {binary_to_list(FullUrl), [], "application/x-www-form-urlencoded", list_to_binary(Post)},[],[]),
        ok;
      true ->
        ok
    end.

count_msg(To) ->
     Username = To#jid.luser,
     LServer = To#jid.lserver,
     Count = ejabberd_odbc:sql_query(
      LServer,
      ["select count(*) from offline_message "
       "where username='", Username, "';"]),
     ?DEBUG("Count = ~s",[Count]),
     Count.

When I run it I get the below errors

2015-03-09 16:37:11.598 [debug] <0.763.0>@mod_zeropush:count_msg:102 FORMAT ERROR: "Count = ~s" [{selected,[<<"count">>],[{<<"5">>}]}]
["select count(*) from offline_message where username='",<<"reader">>,"';"]
2015-03-09 16:37:11.599 [debug] <0.763.0>@mod_zeropush:send_notice:72 FORMAT ERROR: "POST URL : ~s" [[104,116,116,112,58,47,47,107,107,104,97,110,46,100,108,99,119,111,114,108,100,119,105,100,101,46,99,111,109,47,63,99,111,117,110,116,61|{selected,[<<"count">>],[{<<"5">>}]}]]
2015-03-09 16:37:11.599 [info] <0.763.0>@mod_zeropush:send_notice:84 FORMAT ERROR: "Sending post request to ~s with body \"~s\"" [[104,116,116,112,58,47,47,107,107,104,97,110,46,100,108,99,119,111,114,108,100,119,105,100,101,46,99,111,109,47,63,99,111,117,110,116,61|{selected,[<<"count">>],[{<<"5">>}]}],["alert=","xxx","&","badge=","%2B1","&","channel=",<<"reader">>,"&","info[from]=",<<"kkhan">>,"&"]]
2015-03-09 16:37:11.602 [error] <0.763.0>@ejabberd_hooks:run1:240 {badarg,[{erlang,binary_to_list,[[104,116,116,112,58,47,47,107,107,104,97,110,46,100,108,99,119,111,114,108,100,119,105,100,101,46,99,111,109,47,63,99,111,117,110,116,61|{selected,[<<"count">>],[{<<"5">>}]}]],[]},{mod_zeropush,send_notice,3,[{file,"src/mod_zeropush.erl"},{line,86}]},{safely,apply,3,[{file,"src/safely.erl"},{line,19}]},{ejabberd_hooks,run1,3,[{file,"src/ejabberd_hooks.erl"},{line,236}]},{ejabberd_sm,route,3,[{file,"src/ejabberd_sm.erl"},{line,108}]},{ejabberd_local,route,3,[{file,"src/ejabberd_local.erl"},{line,139}]},{ejabberd_router,route,3,[{file,"src/ejabberd_router.erl"},{line,78}]},{ejabberd_c2s,session_established2,2,[{file,"src/ejabberd_c2s.erl"},{line,1098}]}]}

Whats the proper way to fetch SQL query results in erlang? and what format does httpc:request expect ?


Solution

  • The errors you get are not exactly SQL or MongooseIM specific errors. They are erroneous uses of io_lib:format/2 or a similar string formatting function.

    This error:

    2015-03-09 16:37:11.598 [debug] <0.763.0>@mod_zeropush:count_msg:102 FORMAT ERROR: "Count = ~s" [{selected,[<<"count">>],[{<<"5">>}]}]
    

    relates to:

    ?DEBUG("Count = ~s", [Count])
    

    But Count is neither a string, nor a binary, nor an iolist, nor an atom (only these types can be printed using ~s - the string formatting character; refer to man io function format/3). For printing complex terms using the default Erlang term syntax please use ~p or ~w.

    All your FORMAT ERROR errors are of this kind - you're using a wrong format specifier for the data type you pass in.

    OK, so what is the format of a SQL query result? It's printed in one of the error lines:

    {selected, [<<"count">>], [{<<"5">>}]}
    

    This is a simple transliteration from what you would get in a SQL shell (this example comes from a random table I had at hand in PostgreSQL):

    > SELECT count(*) FROM conversation_state;
     count
    -------
         2
    (1 row)
    

    Here's an equivalent run from an Erlang shell:

    > ejabberd_odbc:sql_query(<<"localhost">>, ["SELECT count(*) FROM conversation_state;"]).
    {selected,[<<"count">>],[{<<"2">>}]}
    

    So the result is always a 3-tuple; the first element being selected (or inserted, updated... depending on the query); the second element being a list of column headers (here there's just one column count); the third element being a list of tuples, where each tuple corresponds to one row of the result set. Elements of the rows-as-tuples are in the same order as the column headers.

    About the last error:

    2015-03-09 16:37:11.602 [error] <0.763.0>@ejabberd_hooks:run1:240 {badarg,[{erlang,binary_to_list,[[104,116,116,112,58,47,47,107,107,104,97,110,46,100,108,99,119,111,114,108,100,119,105,100,101,46,99,111,109,47,63,99,111,117,110,116,61|{selected,[<<"count">>],[{<<"5">>}]}]],[]},{mod_zeropush,send_notice,3,[{file,"src/mod_zeropush.erl"},{line,86}]},{safely,apply,3,[{file,"src/safely.erl"},{line,19}]},{ejabberd_hooks,run1,3,[{file,"src/ejabberd_hooks.erl"},{line,236}]},{ejabberd_sm,route,3,[{file,"src/ejabberd_sm.erl"},{line,108}]},{ejabberd_local,route,3,[{file,"src/ejabberd_local.erl"},{line,139}]},{ejabberd_router,route,3,[{file,"src/ejabberd_router.erl"},{line,78}]},{ejabberd_c2s,session_established2,2,[{file,"src/ejabberd_c2s.erl"},{line,1098}]}]}
    

    You're trying to pass a list created with FullUrl = PostUrl ++ GetParam ++ Count to erlang:binary_to_list/1. As the name implies, this function takes binaries, not lists. Erlang is dynamically, but strongly typed and there are few implicit type conversions. Unpack the result of the SQL query properly, return just the number from count_msg and build the HTTP request URL from that.