summaryrefslogtreecommitdiff
path: root/client/conversation.py
blob: 8ec912c7000c9a38596aa35c8def4ea23babc08f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8-*-
from notifier import Notifier
from musicmode import *
from brain import Brain
from mpd import MPDClient


class Conversation(object):

    def __init__(self, persona, mic, profile):
        self.persona = persona
        self.mic = mic
        self.profile = profile
        self.brain = Brain(mic, profile)
        self.notifier = Notifier(profile)

    def delegateInput(self, texts):
        """A wrapper for querying brain."""

        # check if input is meant to start the music module
        for text in texts:
            if any(x in text.upper() for x in ["SPOTIFY", "MUSIC"]):
                # check if mpd client is running
                try:
                    client = MPDClient()
                    client.timeout = None
                    client.idletimeout = None
                    client.connect("localhost", 6600)
                except:
                    self.mic.say(
                        "I'm sorry. It seems that Spotify is not enabled. Please read the documentation to learn how to configure Spotify.")
                    return

                self.mic.say("Please give me a moment, I'm loading your Spotify playlists.")
                music_mode = MusicMode(self.persona, self.mic)
                music_mode.handleForever()
                return

        self.brain.query(texts)

    def handleForever(self):
        """Delegates user input to the handling function when activated."""
        while True:

            # Print notifications until empty
            notifications = self.notifier.getAllNotifications()
            for notif in notifications:
                print notif

            threshold, transcribed = self.mic.passiveListen(self.persona)
            if not transcribed or not threshold:
                continue

            input = self.mic.activeListenToAllOptions(threshold)
            if input:
                self.delegateInput(input)
            else:
                self.mic.say("Pardon?")