Skip to content

Commit

Permalink
join and leave groups
Browse files Browse the repository at this point in the history
  • Loading branch information
estiens committed Oct 7, 2017
1 parent 61f8284 commit b654541
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions .env.sample
Expand Up @@ -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)
14 changes: 14 additions & 0 deletions commands/join_group.rb
@@ -0,0 +1,14 @@
class JoinGroup < SlackRubyBot::Commands::Base
join_regex = /join (?<room>.*)/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
10 changes: 10 additions & 0 deletions 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
6 changes: 5 additions & 1 deletion commands/single_commands.rb
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion commands/what_is_playing.rb
Expand Up @@ -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
10 changes: 6 additions & 4 deletions jukebot.rb
Expand Up @@ -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'
Expand All @@ -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
Expand Down
16 changes: 13 additions & 3 deletions services/sonos_service.rb
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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}")
Expand All @@ -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
Expand Down

0 comments on commit b654541

Please sign in to comment.