zoedsoupe
I’m trying to build a telegram bot with Nadia, and I thought that would be a good idea, to wrap some functionalities with macros, for studying purposes!
Here’re my macros:
#router.ex
defmodule Lovelace.Router
[...]
defp generate_command(command, handler) do
quote do
def do_match_message(
%{
message: %{
text: "/" <> unquote(command)
}
} = var!(update)
) do
handle_message(unquote(handler), [var!(update)])
end
def do_match_message(
%{
message: %{
text: "/" <> unquote(command) <> " " <> _
}
} = var!(update)
) do
handle_message(unquote(handler), [var!(update)])
end
def do_match_message(
%{
message: %{
text: "/" <> unquote(command) <> "@" <> unquote(@bot_name)
}
} = var!(update)
) do
handle_message(unquote(handler), [var!(update)])
end
def do_match_message(
%{
message: %{
text: "/" <> unquote(command) <> "@" <> unquote(@bot_name) <> " " <> _
}
} = var!(update)
) do
handle_message(unquote(handler), [var!(update)])
end
end
end
# Helpers
def handle_message({module, function}, update)
when is_atom(function) and is_list(update) do
Task.start(fn ->
apply(module, function, [hd(update)])
end)
end
def handle_message({module, function}, update)
when is_atom(function) do
Task.start(fn ->
apply(module, function, [update])
end)
end
def handle_message(function, _update)
when is_function(function) do
Task.start(fn ->
function.()
end)
end
def handle_message(_, _), do: nil
## Command
defmacro command(commands, do: function)
when is_list(commands) do
Enum.map(commands, fn command ->
generate_command(command, function)
end)
end
defmacro command(command, do: function) do
generate_command(command, function)
end
defmacro command(commands, module, function)
when is_list(commands) do
Enum.map(commands, fn command ->
generate_command(command, {module, function})
end)
end
defmacro command(command, module, function) do
generate_command(command, {module, function})
end
[...]
end
And I’m using as:
#commands.ex
defmodule Lovelace.Commands do
@moduledoc """
Define all bot's commands
"""
use Lovelace.Router
use Lovelace.Commander
import Lovelace.Helpers
alias Lovelace.Commands.Outside
command "welcome" do
Logger.log(:info, "Command /welcome")
~s(Seja bem vindo ao grupo de Ciência da Computação! )
|> Kernel.<>(
~s|Leia as regras do grupo no pinado e visite nosso GitHub(https://github.com/cciuenf) |
)
|> Kernel.<>(~s(Digite /ajuda para ver mais comandos!))
|> send_message()
end
end
However, when compiling, I’m geeting:
Compiling 2 files (.ex)
== Compilation error in file lib/lovelace/commands.ex ==
** (ArgumentError) expected binary argument in <> operator but got: nil
(elixir 1.11.3) lib/kernel.ex:1821: Kernel.wrap_concatenation/3
(elixir 1.11.3) lib/kernel.ex:1812: Kernel.extract_concatenations/2
(elixir 1.11.3) lib/kernel.ex:1808: Kernel.extract_concatenations/2
(elixir 1.11.3) expanding macro: Kernel.<>/2
lib/lovelace/commands.ex:13: Lovelace.Commands.do_match_message/1
The Error is thrown on
command "welcome"...
in the commands.ex file!
But I ain’t finding where is the error… Could you help me?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
This one works for me:
Demo.f1inside theiexREPL returns the concatenated string for me. On which line in your source are you getting the compilation error? #13 looks like an empty line to me.zoedsoupe
Sorry, I didn’t specified where is the error! I changed the question!
dimitarvp
You changed the bigger code block at the start of your post but that doesn’t tell me where the error is. It still says line 13 in your last coding block, which points at the empty line immediately after
Logger.log.Check this out:
Notice the
13in there. But line 13 is empty. Weird.Can you make one single small module and test with it?
zoedsoupe
The
commands.exfile block here was incomplete, I completed! But I’ll also try to reproduce on another module!dimitarvp
Pasting a bunch of coding blocks (some rather big) doesn’t help much when you have a narrow problem. For such scenarios where more code is involved you’d get much more help if you just made a small GitHub project and linked to it here.
For now, just make one module that demonstrates the problem and we can discuss that.
Eiji
It’s in 2 places:
and
Looks like you don’t have set
@bot_nameinLovelace.Routeror by mistake you calledunquotefor it.zoedsoupe
Thank you! The error wasn’t so specific so I didn’t thought about my
Application.compile_env/2on the beggining ofrouter.ex…