rockneurotiko/ex_gram

[Regex] Problems compiling project when using regexes

thehabbos007 opened this issue ยท 5 comments

This was on 0.8
I was trying to see if i could make regexes work, when compiling my bot with regex("test", :test) in the module, I the following error

** (CompileError) lib/bot/bot.ex: invalid quoted expression: ~r/test/

Please make sure your quoted expressions are made of valid AST nodes. If you would like to introduce a value into the AST, such as a four-element tuple or a map, make sure to call Macro.escape/1 before
    (stdlib) lists.erl:1354: :lists.mapfoldl/3
    (stdlib) lists.erl:1355: :lists.mapfoldl/3
    /mnt/c/Users/Ahmad/Desktop/dev/playground/bot/lib/bot/bot.ex:1: ExGram.Middleware.Builder.__before_compile__/1

Calling Macro.escape on the Regex.compile! function in

@regexes [regex: Regex.compile!(unquote(regex)), name: unquote(name)]
seemed to fix this.

I'll gladly make a PR and update some of the readme for regexes if this change won't break anything ๐Ÿ˜„

Hi!

Thanks for open the issue ๐Ÿ˜„

You are 100% right, this is not working properly ๐Ÿคฆโ€โ™€

I've taken a look, and I think that you can add Macro.escape in the __before__compile__ method in ExGram.Middleware.Builder, and you can add it to the three settings (middlewares, commands and regexes), for example:

regexes = Module.get_attribute(env.module, :regexes) |> Enum.reverse() |> Macro.escape()

Also, we can benefit of this and improve the regex macro, so that it detects if you already pass a regex to not compile it (so, you can use regex(~r/test/, :test))

It could be something like this:

defmacro regex(regex, name, _opts \\ []) do
    quote do
      @regexes [
        regex: ExGram.Middleware.Builder.compile_regex(unquote(regex)),
        name: unquote(name)
      ]
    end
  end

  def compile_regex(%{__struct__: Regex} = regex), do: regex
  def compile_regex(binary) when is_binary(binary), do: Regex.compile!(binary)

I'll let you do the PR if you want ๐Ÿ˜„

Thank you, I will get on it right away! ๐Ÿ˜„

I will add this to the release 0.8.1, I'll release it as soon as we merge #39

Great! Also, i do not know if this is reproducible problem, but I ran into errors when sending answers throguh the bot, i get the following log item

16:56:50.215 [info]  POST https://api.telegram.org/bot/sendMessage -> 404 (208.026 ms)

Seems like api key is not appended in the bot path in sendMessage calls, and somehow simply moving the token config fetching to above the path assignment https://github.com/rockneurotiko/ex_gram/blob/master/lib/ex_gram/macros.ex#L410
seems to fix this for me. Do you believe this is an issue worth opening?

๐Ÿค” That actually is something that I've never experienced in my bots.

Let's open an issue, and if you can specify how are you sending answers and how you say that the fix will work?

Thanks for reporting all of this! This library just need more users to polish it โค๏ธ