Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bot name in help handler #89

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions lib/alice/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ defmodule Alice.Conn do
user_data(conn).name
end

@doc """
Returns the name of the bot
"""
def bot_name(conn), do: conn.slack.me.name
Copy link
Member

Choose a reason for hiding this comment

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

prefer this format (like the other functions in here):

def bot_name(conn = %Conn{}) do
  conn.slack.me.name
end

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 fixed


@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
2 changes: 1 addition & 1 deletion test/alice/handlers/help_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Alice.Handlers.HelpTest do
alias Alice.Handlers.TestHandler

test "help_for_handler generates the correct output" do
assert Help.help_for_handler(TestHandler) ==
assert Help.help_for_handler(TestHandler, %{slack: %{me: %{name: "alice"}}}) ==
Copy link
Member Author

Choose a reason for hiding this comment

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

@adamzaninovich not a fan of this hard-coded conn. wasn't sure if there was a cleaner way to do this in tests?

Copy link
Member

Choose a reason for hiding this comment

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

maybe just use Conn.new and pass in the slack component

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea I like, that at least hides the use of the slack key to the internals until that gets changed with the whole different backend stuff.

"""
>*Alice.Handlers.TestHandler*
>
Expand Down