Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/alice/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ defmodule Alice.Conn do
user_data(conn).name
end

@doc """
Returns the name of the bot
"""
def bot_name(conn = %Conn{}) do
conn.slack.me.name
end

@doc """
Builds a string to use as an @reply back to the user who sent the message
"""
Expand Down
44 changes: 22 additions & 22 deletions lib/alice/handlers/help.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ defmodule Alice.Handlers.Help do
def general_help(conn) do
["_Here are all the handlers I know about…_",
handler_list(),
"_Get info about a specific handler with_ `@alice help <handler name>`",
"_Get info about all handlers with_ `@alice help all`",
"_Get info about a specific handler with_ `@#{Conn.bot_name(conn)} help <handler name>`",
"_Get info about all handlers with_ `@#{Conn.bot_name(conn)} help all`",
"_Feedback on Alice is appreciated. Please submit an issue at https://github.com/alice-bot/alice/issues _"]
|> Enum.join("\n\n")
|> reply(conn)
Expand All @@ -31,7 +31,7 @@ defmodule Alice.Handlers.Help do
defp do_keyword_help(conn, "all") do
[@pro_tip,
"_Here are all the routes and commands I know about…_"
| Enum.map(Router.handlers, &help_for_handler/1)]
| Enum.map(Router.handlers, &help_for_handler/2)]
|> Enum.reduce(conn, &reply/2)
end
defp do_keyword_help(conn, term) do
Expand Down Expand Up @@ -78,54 +78,54 @@ defmodule Alice.Handlers.Help do
defp deliver_help(handler, conn) do
[ @pro_tip,
~s(_Here are all the routes and commands I know for "#{get_term(conn)}"_),
help_for_handler(handler) ]
help_for_handler(handler, conn) ]
|> Enum.join("\n\n")
|> reply(conn)
end

def help_for_handler(handler) do
def help_for_handler(handler, conn) do
[ ">*#{path_name(handler)}*",
format_routes("Routes", handler.routes, handler),
format_routes("Commands", handler.commands, handler), "" ]
format_routes("Routes", handler.routes, handler, conn),
format_routes("Commands", handler.commands, handler, conn), "" ]
|> compact()
|> Enum.join("\n")
end

defp path_name("Elixir." <> name), do: name
defp path_name(handler), do: handler |> to_string() |> path_name()

defp format_routes(_,[],_), do: nil
defp format_routes(title, routes, handler) do
defp format_routes(_,[],_,_), do: nil
defp format_routes(title, routes, handler, conn) do
routes = Enum.map(routes, fn({_,name}) -> name end)

docs = handler
|> Code.get_docs(:docs)
|> Stream.map(fn({{name,_},_,_,_,text}) -> {title, name, text} end)
|> Stream.filter(fn({_,name,_}) -> name in routes end)
|> Enum.map(&format_route/1)
|> compact()
|> Code.get_docs(:docs)
|> Stream.map(fn({{name,_},_,_,_,text}) -> {title, name, text, conn} end)
|> Stream.filter(fn({_,name,_,conn}) -> name in routes end)
|> Enum.map(&format_route/1)
|> compact()

[">", "> *#{title}:*" | docs]
|> Enum.join("\n")
end

defp format_route({_,_,false}), do: nil
defp format_route({title, name, text}) do
["> _#{name}_", format_text(text, title)]
defp format_route({_,_,false,_}), do: nil
defp format_route({title, name, text, conn}) do
["> _#{name}_", format_text(text, title, conn)]
|> Enum.join("\n")
end

defp format_text(nil,_), do: "> _no documentation provided_"
defp format_text(text, title) do
defp format_text(nil,_,_), do: "> _no documentation provided_"
defp format_text(text, title, conn) do
text
|> String.trim()
|> String.split("\n")
|> Stream.map(fn(line) -> "> #{prefix_command(title, line)}" end)
|> Stream.map(fn(line) -> "> #{prefix_command(title, line, conn)}" end)
|> Enum.join("\n")
end

defp prefix_command("Commands", "`" <> line), do: "`@alice #{line}"
defp prefix_command(_, line), do: line
defp prefix_command("Commands", "`" <> line, conn), do: "`@#{Conn.bot_name(conn)} #{line}"
defp prefix_command(_, line, _), do: line

defp compact(list), do: list |> Enum.reject(&is_nil/1)
end
15 changes: 12 additions & 3 deletions test/alice/handlers/help_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ defmodule Alice.Handlers.HelpTest do
alias Alice.Handlers.TestHandler

test "help_for_handler generates the correct output" do
assert Help.help_for_handler(TestHandler) ==
conn = Alice.Conn.make(%{}, %{me: %{name: "alice"}})
assert Help.help_for_handler(TestHandler, conn) == test_help_message_output(conn)
end

test "help_for_handler generates the correct output with a different bot name" do
conn = Alice.Conn.make(%{}, %{me: %{name: "mad_hatter"}})
assert Help.help_for_handler(TestHandler, conn) == test_help_message_output(conn)
end

defp test_help_message_output(%Alice.Conn{slack: %{me: %{name: bot_name}}}) do
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm maybe this should just use the Conn.bot_name on the passed in Conn, instead of deconstructing using the slack key again?

"""
>*Alice.Handlers.TestHandler*
>
Expand All @@ -14,9 +23,9 @@ defmodule Alice.Handlers.HelpTest do
>
> *Commands:*
> _command1_
> `@alice cmd1`: does some stuff
> `@#{bot_name} cmd1`: does some stuff
> _command2_
> `@alice cmd2`: does some other stuff
> `@#{bot_name} cmd2`: does some other stuff
> also this one is multiline
> _command3_
> this one doesn't start with an
Expand Down