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

Add dishwasher manager bot #2

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
121e253
Avoid showing menu for other bots
rhumbertgz Sep 11, 2017
4d2e0d3
Fix warnings for unused arguments
rhumbertgz Sep 11, 2017
21e32ea
Update dependencies to avoid warning on Elixir 1.5
rhumbertgz Sep 11, 2017
b0d7bfc
Add initial version of the dishwasher-manager bot
rhumbertgz Sep 11, 2017
4a7fd86
Add dishwasher-manager bot workers
rhumbertgz Sep 11, 2017
dc2c120
Update dependencies
rhumbertgz Sep 11, 2017
73978c6
Add beta dishwasher-manager bot
rhumbertgz Sep 11, 2017
bd2aab7
Add support for individual message responses
rhumbertgz Sep 11, 2017
b88baa5
Add support for multiple help menus based on user's role
rhumbertgz Sep 11, 2017
bf821e6
Fix minor bugs
rhumbertgz Sep 11, 2017
9284cbb
Add support for private messages
rhumbertgz Sep 11, 2017
757ff2f
Add support for private messages
rhumbertgz Sep 11, 2017
6d0fb1e
Full beta version
rhumbertgz Sep 13, 2017
8c72396
Add dishwasher scheduler
rhumbertgz Sep 13, 2017
1afd693
Add scheduler supervisor
rhumbertgz Sep 13, 2017
ef21e27
Add timezone parameter for cronex
rhumbertgz Sep 13, 2017
c391c56
Update dependencies
rhumbertgz Sep 13, 2017
7d3fdfd
Update internal data structure for users and schedule
rhumbertgz Sep 14, 2017
23077cf
Add initial values to users and schedule file
rhumbertgz Sep 14, 2017
214b771
Remove print trace
rhumbertgz Sep 14, 2017
c29bf13
Format code
rhumbertgz Sep 14, 2017
6a3d52f
Add dishwasher settings
rhumbertgz Sep 14, 2017
816c2f6
Add github settings
rhumbertgz Sep 14, 2017
838f6c9
Update scheduler messages
rhumbertgz Sep 14, 2017
3ceaa05
Fix next manager functionality bug
rhumbertgz Sep 14, 2017
1c44e3d
Fix warnings
rhumbertgz Sep 14, 2017
69c2e10
Fix typo on the reminder message for the next dishwasher manager
rhumbertgz Sep 14, 2017
85ce1da
Fix bugs on message patterns and generalize them
rhumbertgz Sep 14, 2017
7cb5fb4
Update dependencies
rhumbertgz Mar 27, 2018
d02f285
Fix error after user-name changes
rhumbertgz Mar 27, 2018
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
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ config :logger,
handle_otp_reports: true,
handle_sasl_reports: true

config :cronex,
timezone: "Europe/Copenhagen"

import_config "#{Mix.env}.secret.exs"
85 changes: 85 additions & 0 deletions data/dishwasher_manager/schedule.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
[{cderoove,{<<"Coen De Roover">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 11,month => 9,year => 2017}}},
{mathsaey,{<<"Mathijs Saey">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 18,month => 9,year => 2017}}},
{thierry,{<<"Thierry Renaux">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 25,month => 9,year => 2017}}},
{kevin,{<<"Kevin Pinte">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 2,month => 10,year => 2017}}},
{fmyter,{<<"Florian Myter">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 9,month => 10,year => 2017}}},
{tmoldere,{<<"Tim Molderez">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 16,month => 10,year => 2017}}},
{joeri,{<<"Joeri De Koster">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 23,month => 10,year => 2017}}},
{simon,{<<"Simon Van de Water">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 30,month => 10,year => 2017}}},
{nathalie,{<<"Nathalie Oostvogels">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 6,month => 11,year => 2017}}},
{jswalens,{<<"Janwillem Swalens">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 13,month => 11,year => 2017}}},
{eljenso,{<<"Jens Nicolay">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 20,month => 11,year => 2017}}},
{kennedy,{<<"Kennedy Kambona">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 27,month => 11,year => 2017}}},
{qstieven,{<<"Quentin Stievenart">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 4,month => 12,year => 2017}}},
{dirk,{<<"Dirk van Deun">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 11,month => 12,year => 2017}}},
{jbsartor,{<<"Jennifer Sartor">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 18,month => 12,year => 2017}}},
{jdeblese,{<<"Jonas De Bleser">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 8,month => 1,year => 2018}}},
{nvgeele,{<<"Nils Van Geele">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 15,month => 1,year => 2018}}},
{ascullpu,{<<"Angel Luis Scull Pupo">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 22,month => 1,year => 2018}}},
{carmen,{<<"Carmen Torres Lopez">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 29,month => 1,year => 2018}}},
{jezaman,{<<"Jesse Zaman">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 5,month => 2,year => 2018}}},
{mvdcamme,{<<"Maarten Vandercammen">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 12,month => 2,year => 2018}}},
{lachrist,{<<"Laurent Christophe">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 19,month => 2,year => 2018}}},
{marmat,{<<"Matteo Marra">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 26,month => 2,year => 2018}}},
{noah,{<<"Noah Van Es">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 5,month => 3,year => 2018}}},
{svdvonde,{<<"Sam Van den Vonder">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 12,month => 3,year => 2018}}},
{humberto,{<<"Humberto Rodriguez">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 19,month => 3,year => 2018}}},
{cdetroye,{<<"Christophe De Troyer">>,
#{'__struct__' => 'Elixir.Date',calendar => 'Elixir.Calendar.ISO',
day => 26,month => 3,year => 2018}}},
{elisagboix,{<<"Elisa Gonzalez">>,
#{'__struct__' => 'Elixir.Date',
calendar => 'Elixir.Calendar.ISO',day => 2,month => 4,
year => 2018}}}].
28 changes: 28 additions & 0 deletions data/dishwasher_manager/users.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[{cderoove,<<"Coen De Roover">>},
{mathsaey,<<"Mathijs Saey">>},
{thierry,<<"Thierry Renaux">>},
{kevin,<<"Kevin Pinte">>},
{fmyter,<<"Florian Myter">>},
{tmoldere,<<"Tim Molderez">>},
{joeri,<<"Joeri De Koster">>},
{simon,<<"Simon Van de Water">>},
{nathalie,<<"Nathalie Oostvogels">>},
{jswalens,<<"Janwillem Swalens">>},
{eljenso,<<"Jens Nicolay">>},
{kennedy,<<"Kennedy Kambona">>},
{qstieven,<<"Quentin Stievenart">>},
{dirk,<<"Dirk van Deun">>},
{jbsartor,<<"Jennifer Sartor">>},
{jdeblese,<<"Jonas De Bleser">>},
{nvgeele,<<"Nils Van Geele">>},
{ascullpu,<<"Angel Luis Scull Pupo">>},
{carmen,<<"Carmen Torres Lopez">>},
{jezaman,<<"Jesse Zaman">>},
{mvdcamme,<<"Maarten Vandercammen">>},
{lachrist,<<"Laurent Christophe">>},
{marmat,<<"Matteo Marra">>},
{noah,<<"Noah Van Es">>},
{svdvonde,<<"Sam Van den Vonder">>},
{humberto,<<"Humberto Rodriguez">>},
{cdetroye,<<"Christophe De Troyer">>},
{elisagboix,<<"Elisa Gonzalez">>}].
21 changes: 16 additions & 5 deletions lib/behaviours/plugin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,29 @@ defmodule Plugin do
GenServer.start_link(__MODULE__, [args], [{:name, __MODULE__}])
end

# "Handling indidual messages"
def handle_info(message = %{channel: nil, type: "message", text: text, user: from}, state) do
reply = on_message(text, :dishwasher_app, from)
Copy link
Owner

Choose a reason for hiding this comment

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

This should not be hardcoded in here?


case reply do
{:ok, reply} -> SlackManager.send_private_message("#{reply}", from)
{:error, reason} -> IO.puts "Error in plugin #{reason}"
{:noreply} -> :noop
end

{:noreply, state}
end

def handle_info(message = %{type: "message", text: text, user: from}, state) do
reply = on_message(text, message.channel, from)

case reply do
{:ok, reply} ->
SlackManager.send_message("#{reply}", message.channel)
{:error, reason} ->
IO.puts "Error in plugin #{reason}"
{:noreply} ->
:noop
{:error, reason} -> IO.puts "Error in plugin #{reason}"
{:noreply} -> :noop
end

{:noreply, state}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bot/benvolios.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Bot.Benvolios do
end

# Prints out help message.
def on_message(<<"help"::utf8>>, _channel, _sender) do
def on_message(<<"help"::utf8>>, @channel, _sender) do
res = """
```
order : Order something.
Expand Down
225 changes: 225 additions & 0 deletions lib/bot/diswasher_manager.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
defmodule Bot.DiswasherManager do
require Logger
use Plugin

@boss Application.fetch_env!(:slack, :diswasher_duties_owner)
@channel Application.fetch_env!(:slack, :diswasher_duties_channel)

# The message "swap_with @x" is used to swap weekly duties with the user `x`.
def on_message(<<"swap_with"::utf8, user::bitstring>>, _channel, sender) do
user = String.trim(user)
case Brain.DishwasherManager.swap_duties(sender, user) do
:ok -> SlackManager.send_private_message("You swapped your dishwasher duties with @#{sender}.", user)
{:ok, "Duties swapped!"}
{:error, msg} -> {:ok, msg}
end
end

# The message "manager?" return the name uf the current dishwasher manager
def on_message(<<"manager?"::utf8>>, _channel, _sender) do
{:ok, manager, fullname} = Brain.DishwasherManager.manager?()

case manager do
:no_specified -> {:ok, "The schedule has not been created. Use the command 'help' for more information."}
name -> {:ok, "The current dishwasher manager is `#{fullname}`"}
end
end

def on_message(<<"when?"::utf8>>, _channel, sender) do
{:ok, fullname, startDate} = Brain.DishwasherManager.when?(sender)

case fullname do
:invalid_user -> {:ok, "The schedule has not been created. Use the command 'help' for more information."}

_ -> from = Date.to_string(startDate)
to = startDate |> Date.add(4) |> Date.to_string()
{:ok, "Your next dishwasher duties will be from `#{from}` to `#{to}`"}
end
end

# Prints out all the outstanding orders.
# Restricted to admins only.
def on_message(<<"schedule"::utf8, _::bitstring>>, _channel, _sender) do
{:ok, schedule} = Brain.DishwasherManager.schedule()
build_schedule_list(schedule)
end

# Prints out all the outstanding orders.
# Restricted to admins only.
def on_message(<<"create_schedule"::utf8, startDate::bitstring>>, _channel, @boss) do
{:ok, schedule} = Brain.DishwasherManager.create_schedule(startDate)
build_schedule_list(schedule)
end

def on_message(<<"users"::utf8, _::bitstring>>, _channel, @boss) do
{:ok, users} = Brain.DishwasherManager.users()
build_user_list(users)
end

def on_message(<<"add_user"::utf8, userDetails::bitstring>>, _channel, @boss) do
case String.split(userDetails) do
[] -> {:noreply}
[user| fullname] -> :ok = Brain.DishwasherManager.add_user(user, fullname)
{:ok, "The user {#{user}, #{fullname}} has been saved."}
end
end

def on_message(<<"remove_user"::utf8, user::bitstring>>, _channel, @boss) do
:ok = Brain.DishwasherManager.remove_user(user)
{:ok, "The user #{user} has been removed."}
end

def on_message(<<"remove_users"::utf8>>, _channel, @boss) do
:ok = Brain.DishwasherManager.remove_users()
{:ok, "The user list was removed."}
end

def on_message(<<"remove_schedule"::utf8>>, _channel, @boss) do
:ok = Brain.DishwasherManager.remove_schedule()
{:ok, "The schedule was removed."}
end

def on_message(<<"set_manager"::utf8>>, _channel, @boss) do
{:ok, manager} = Brain.DishwasherManager.set_manager_of_the_week()
case manager do
:no_specified -> {:ok, "The schedule has not been created. Use the command 'help' for more information."}
name -> {:ok, "The current dishwasher manager is `#{name}`"}
end
end

def on_message(<<"wave"::utf8>>, :dishwasher_app, _sender) do
wave_dishwasher_manager()
end

def on_message(<<"wave"::utf8>>, :channel, _sender) do
wave_dishwasher_manager()
end

def on_message(<<":wave:"::utf8>>, :dishwasher_app, _sender) do
wave_dishwasher_manager()
end


def on_message(<<":wave:"::utf8>>, :channel, _sender) do
wave_dishwasher_manager()
end

defp wave_dishwasher_manager do
{:ok, manager, fullname} = Brain.DishwasherManager.manager?()

case manager do
:no_specified -> {:ok, "As the schedule has not been created, there is not Dishwasher Manager."}
name -> SlackManager.send_private_message(":wave: Hey Dishwasher Manager! Please do your dishwasher duties as soon as you can.", manager)
{:ok, "The current dishwasher manager `#{fullname}` was :wave:"}
end
end

# Prints out help message.
def on_message(<<"help"::utf8>>, @channel, @boss) do
res = boss_help_menu()
{:ok, res}
end

# Prints out help message.
def on_message(<<"help"::utf8>>, @channel, _sender) do
res = user_help_menu()
{:ok, res}
end

# Prints out help message.
def on_message(<<"help"::utf8>>, :dishwasher_app, @boss) do
res = boss_help_menu()
{:ok, res}
end

def on_message(<<"help"::utf8>>, :dishwasher_app, _sender) do
res = user_help_menu()
{:ok, res}
end


# def on_message(_text, @channel, _sender) do
# {:ok, general_msg()}
# end
#
# def on_message(_text, :dishwasher_app, _sender) do
# {:ok, general_msg()}
# end

def on_message(_text, _channel, _sender) do
{:noreply}
end


# defp general_msg do
# """
# Hello Softie! Please use the command `help` for more information.
# """
# end

defp build_schedule_list(schedule) when schedule == %{} , do: {:ok, "There is no schedule ready. Use the command 'help' for more information."}

defp build_schedule_list(schedule) do
resp = schedule
|> Enum.map(fn {_k,{fullname, from}} ->
"- #{fullname} -> from: #{from} to: #{Date.add(from, 4)}" end)
|> Enum.join("\n")

{:ok, "```#{resp}```"}
end

defp build_user_list(users) when users == %{} , do: {:ok, "The list of user is empty. Use the command 'help' for more information."}

defp build_user_list(users) do
resp = users
|> Enum.map(fn {user, fullname} -> "- #{fullname} (@#{user})" end)
|> Enum.join("\n")

{:ok, "```#{resp}```"}
end

defp user_help_menu do
"""
```
swap_with : Swaps weekly duties with another person.
Example: "swap_with @cdtroye".
manager? : Shows the current dishwasher manager.
Example: "manager?"
schedule : Shows the current dishwasher schedule.
Example: "schedule"
wave : Sends a notification to the current diswasher manager in case he has to do his duties.
Example: "wave" or ":wave:"
when? : Shows the dates of your the next diswasher duties.
Example: "when? "
```

"""
end

defp boss_help_menu do
"""
```
swap_with : Swaps weekly duties with another person.
Example: "swap_with @cdtroye".
manager? : Shows the current dishwasher manager.
Example: "manager?"
schedule : Shows the current dishwasher schedule.
Example: "schedule"
create_schedule : Creates a dishwasher schedule starting from the date specified.
Example: "create_schedule 2017-09-07"
users : Shows the current list of users.
Example: "users"
add_user : Add a new user. The first argument is the "slack user name",
followed by its fullname.
Example: "add_user @humberto Humberto Rodriguez Avila"
remove_user : Remove an user.
Example: "remove_user @humberto"
set_manager : Set the manager for the current week.
Example: "set_manager"
```

"""
end


end
Loading