Selaa lähdekoodia

#1 slack input/output bots

Martin 10 vuotta sitten
vanhempi
commit
f19a06a82c

+ 1 - 0
README.md

@@ -8,6 +8,7 @@ Python requirements:
     gTTS
     requests
     arrow
+    slackclient
 
 PortAudio: 
     linux: http://portaudio.com/download.html, download, configure, make, make install

+ 6 - 2
voice_control/conf.py

@@ -1,5 +1,9 @@
 import os
 
-_HOME = os.getenv('HOME')
+_VC_DIR = os.path.dirname(os.path.abspath(__file__))
 
-CLIPS_FOLDER = os.path.join(_HOME, 'workspaces', 'visual-code', 'voice_control', 'resources', 'clips')
+CLIPS_FOLDER = os.path.join(_VC_DIR, '..', 'resources', 'clips')
+
+SLACK_BOT_API_KEY = 'xoxb-124589937168-ljmRwQODWQ2R1d7eIs99hAc1'
+
+MUSIC_FOLDER = '/mnt/nas/music/albums'

+ 9 - 1
voice_control/input_sources/__init__.py

@@ -2,6 +2,7 @@ import Queue
 import threading
 import console
 import google_cloud_speech
+import slack_bot
 
 LANGUAGE = 'en-GB'
 PREFERRED_PHRASES = None
@@ -31,7 +32,8 @@ def launch_input_sources():
 
     services = [
         console.ConsoleInput(_TOKEN_STREAM),
-        google_cloud_speech.GoogleCloudSpeech(_TOKEN_STREAM),
+        #google_cloud_speech.GoogleCloudSpeech(_TOKEN_STREAM),
+        slack_bot.SlackBot(_TOKEN_STREAM),
     ]
 
     for s in services:
@@ -40,15 +42,21 @@ def launch_input_sources():
         t.start()
 
 def get_token(timeout=120, preferred_phrases=None):
+    global PREFERRED_PHRASES
+    PREFERRED_PHRASES = preferred_phrases
     return _TOKEN_STREAM.get_token(timeout)
 
 def get_line(timeout=120, preferred_phrases=None):
+    global PREFERRED_PHRASES
+    PREFERRED_PHRASES = preferred_phrases
     return _TOKEN_STREAM.get_line(timeout)
 
 def token_available():
     return not _TOKEN_STREAM.buffer.empty()
 
 def get_until_terminated(timeout=120, terminators=['done', 'cancel'], preferred_phrases=None):
+    global PREFERRED_PHRASES
+    PREFERRED_PHRASES = preferred_phrases
     res = []
     t = get_token(timeout=timeout, preferred_phrases=preferred_phrases)
     while t not in terminators:

+ 31 - 0
voice_control/input_sources/slack_bot.py

@@ -0,0 +1,31 @@
+import logging
+import time
+import slackclient
+import base
+import conf
+import output
+
+MAX_EVENT_AGE = 30 # seconds
+
+class SlackBot(base.InputSource):
+    def __init__(self, stream):
+        self.stream = stream
+        self.client = slackclient.SlackClient(conf.SLACK_BOT_API_KEY)
+
+    def run(self):
+        if self.client.rtm_connect():
+            while True:
+                events = self.client.rtm_read()
+                for e in events:
+                    logging.debug(e)
+                    if e.get('type') == 'hello':
+                        output.debug('Slack input bot: connected.')
+                    if e.get('type') == 'message' and e.get('channel', '').startswith('D') and e.get('text') and float(e['ts']) > time.time() - MAX_EVENT_AGE:
+                        for token in e.get('text').split(' '):
+                            self.stream.add_token(token)
+                    
+                    logging.debug('Slack event received: %s', e)
+
+
+        else:
+            output.failure('Slack input bot: RTM connection failed.')

+ 3 - 1
voice_control/output/__init__.py

@@ -1,10 +1,12 @@
 import logging
 import audio_output
 import console
+import slack_bot
 
 SERVICES = [
-    audio_output.AudioOutput(),
+    #audio_output.AudioOutput(),
     console.Console(),
+    slack_bot.SlackBot(),
 ]
 
 def success():

+ 40 - 0
voice_control/output/slack_bot.py

@@ -0,0 +1,40 @@
+import logging
+import slackclient
+import base
+import conf
+
+
+class SlackBot(base.Base):
+    def __init__(self):
+        self.client = slackclient.SlackClient(conf.SLACK_BOT_API_KEY)
+
+    def _send(self, text):
+        res = self.client.api_call(
+            'chat.postMessage',
+            channel='voice_control',
+            text=text
+        )
+
+        logging.debug(res)
+
+
+    def success(self):
+        self._send('Ok.')
+    
+    def failure(self, s=None):
+        if s:
+            self._send(s)
+        else:
+            self._send('Error.')
+
+    def cancelled(self):
+        self._send('Cancelled.')
+    
+    def tts(self, s):
+        self._send(s)
+
+    def text(self, s):
+        self._send(s)
+
+    def debug(self, s):
+        self._send(s)