From b654541f395f19c7ad47d9515aee2e525ba77674 Mon Sep 17 00:00:00 2001 From: Eric Stiens Date: Sat, 7 Oct 2017 14:19:59 -0500 Subject: [PATCH] join and leave groups --- .env.sample | 1 + commands/join_group.rb | 14 ++++++++++++++ commands/leave_group.rb | 10 ++++++++++ commands/single_commands.rb | 6 +++++- commands/what_is_playing.rb | 4 +++- jukebot.rb | 10 ++++++---- services/sonos_service.rb | 16 +++++++++++++--- 7 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 commands/join_group.rb create mode 100644 commands/leave_group.rb diff --git a/.env.sample b/.env.sample index edadd20..36c3abb 100644 --- a/.env.sample +++ b/.env.sample @@ -4,3 +4,4 @@ RSPOTIFY_PASSWORD (spotify app password) USERNAME (username for sonos api) PASSWORD (password for sonos api) NODE_SONOS_HTTP_API_URL (url for sonos api) +SLACK_RUBY_BOT_ALIASES (any aliases you want jukebot to respond to) diff --git a/commands/join_group.rb b/commands/join_group.rb new file mode 100644 index 0000000..9cc0589 --- /dev/null +++ b/commands/join_group.rb @@ -0,0 +1,14 @@ +class JoinGroup < SlackRubyBot::Commands::Base + join_regex = /join (?.*)/i + match BotRegex.new(join_regex) + + def self.call(client, data, match) + room = match[:room] + if JukeBot.api.join(room) + response = "Alright, we're now part of a group with #{room}" + else + response = "Sorry, that doesn't appear to be a room I can join." + end + client.say(text: response, channel: data.channel) + end +end diff --git a/commands/leave_group.rb b/commands/leave_group.rb new file mode 100644 index 0000000..bc35b2f --- /dev/null +++ b/commands/leave_group.rb @@ -0,0 +1,10 @@ +class LeaveGroup < SlackRubyBot::Commands::Base + command 'leave' + command 'sandalone' + + def self.call(client, data, _match) + JukeBot.api.leave + response = "Alright, we're all on our own in #{JukeBot.api.current_room}" + client.say(text: response, channel: data.channel) + end +end diff --git a/commands/single_commands.rb b/commands/single_commands.rb index 1143f00..e8a0a30 100644 --- a/commands/single_commands.rb +++ b/commands/single_commands.rb @@ -30,7 +30,11 @@ class JukeBot < SlackRubyBot::Bot end command 'rooms' do |client, data, _match| - response = "Current rooms available for control are #{api.rooms}" + rooms = api.rooms + response = "There's currently #{rooms.length} groups of speakers." + rooms.each_with_index do |room, idx| + response += "\nGroup #{idx + 1}: #{room.join(', ')}" + end client.say(text: response, channel: data.channel) end diff --git a/commands/what_is_playing.rb b/commands/what_is_playing.rb index fb54959..e4bed02 100644 --- a/commands/what_is_playing.rb +++ b/commands/what_is_playing.rb @@ -16,7 +16,9 @@ def self.call(client, data, _match) current_track = JukeBot.api.current_track artist = current_track[:artist] title = current_track[:title] - response = "#{title} by #{artist}" + image = current_track[:absoluteAlbumArtUri] + response = "#{title} by #{artist}]" + response += "\n #{image}" if image client.say(text: response, channel: data.channel) end end diff --git a/jukebot.rb b/jukebot.rb index 7b504c8..621b5fa 100644 --- a/jukebot.rb +++ b/jukebot.rb @@ -6,6 +6,10 @@ require_relative 'includes/bot_regex' require_relative 'includes/string_monkeypatch' +require_relative 'services/sonos_service' +require_relative 'services/spotify_service' +require_relative 'services/tune_in_service' + require_relative 'commands/what_is_playing' require_relative 'commands/find_music' require_relative 'commands/play_music' @@ -14,10 +18,8 @@ require_relative 'commands/single_commands' require_relative 'commands/find_radio' require_relative 'commands/play_radio' - -require_relative 'services/sonos_service' -require_relative 'services/spotify_service' -require_relative 'services/tune_in_service' +require_relative 'commands/join_group' +require_relative 'commands/leave_group' class JukeBot < SlackRubyBot::Bot def self.api diff --git a/services/sonos_service.rb b/services/sonos_service.rb index a25924b..c9a5d62 100644 --- a/services/sonos_service.rb +++ b/services/sonos_service.rb @@ -5,7 +5,7 @@ class Sonos API_COMMANDS = %i[mute unmute pause play groupMute groupUnmute togglemute state next previous favorite favorites playlist lockvolumes unlockvolumes repeat shuffle crossfade pauseall resumeall - clearqueue linein].freeze + clearqueue linein leave].freeze attr_reader :current_room @@ -15,7 +15,7 @@ def initialize(sonos_room: 'Bedroom') end def change_room(room) - return false unless rooms.include?(room.downcase) + return false unless room_list.include?(room.downcase) @current_room = room @api = create_api_call end @@ -28,6 +28,11 @@ def method_missing(method, *args, &block) end end + def join(room) + return false unless room_list.include?(room.downcase) + @api.join.get(URI.encode(room)) + end + # when can be 'now', 'next', or 'queue' def spotify_play(track:, when_to_play: 'now') @api.spotify.get("#{when_to_play}/#{track}") @@ -49,8 +54,13 @@ def change_group_volume(volume) @api.groupvolume.get(volume) end + def room_list + rooms.flatten.uniq + end + def rooms - @api.zones.get.map { |z| z.coordinator.roomName }.map(&:downcase) + groups = @api.zones.get.map(&:members) + groups.map { |group| group.map { |g| g.roomName.downcase } } end def current_track