tryelixir
tryelixir copied to clipboard
Discord Bot integration
Hi, in the Elixir Discord, there was discussion about trying to get an Elixir REPL running through a chat bot. Would you be against a chat bot proxying the REPL through your hosted instance?
You can join in on the discussion here, in #off-topic.
Would you be against a chat bot proxying the REPL through your hosted instance?
You can use this, I'm ok with that.
Keep in mind that this project was always meant to be an introduction to elixir, basically give someone a chance to try and learn a bit about the language without having to install it locally. It was never meant to provide the full experience, because of that, the REPL is somewhat limited, users aren't allowed to run any code.
The sandbox works by parsing the AST and checking for forbidden code, if it finds any, an error will be shown to the user. Expect bugs, I have more security layers running in the server that should stop users if they break from the elixir sandbox, or at least stop them from doing anything nasty. But it's definitely possible that someone finds a way to bring the elixir application down, see #10.
Anyway, this is why I called the project Try Elixir and not Elixir Playground. I think a more interesting design would be sandboxing code the way the Go Playground or the Rust Playground do.
If you still want to use this, here's how:
POST /api/eval
, with the parameter code
that should be a string containing the code that you want to execute. For example:
curl \
--request POST \
--cookie cookie_jar \
--cookie-jar cookie_jar \
--data-urlencode 'code="Elixir" |> String.graphemes() |> Enum.frequencies()' \
https://tryelixir.net/api/eval
You should get the following JSON response:
{
"output": "",
"prompt": "iex(2)> ",
"result": "%{\"E\" => 1, \"i\" => 2, \"l\" => 1, \"r\" => 1, \"x\" => 1}",
"warnings": []
}
You can ignore the prompt
field. output
should contain the captured stdout
, result
the result of evaluating the code, and warnings
is a list of warnings produced by the elixir compiler. If the evaluation produces an error, there will be an error
field in the response. For example:
curl \
--request POST \
--cookie cookie_jar \
--cookie-jar cookie_jar \
--data-urlencode 'code=x' \
https://tryelixir.net/api/eval
{
"error": "** (CompileError) iex:2: undefined function x/0",
"output": "",
"prompt": "iex(3)> ",
"warnings": ["variable \"x\" does not exist and is being expanded to \"x()\", please use parentheses to remove the ambiguity or change the variable name"]
}
You need to keep track of the cookie, because that's how the application keeps track of your session, like variables, user defined modules, etc. The session is cleared after 5 minutes of inactivity or if the given code takes too long to evaluate.
Let me know if there's anything I can change in the response format that makes you life easier.