Exemple #1
0
def distress_call(ex):
    """Broadcast a distress call"""
    tts.say(["Alert! " + str(ex) + ". Please check urgently!"])
    traces = traceback.format_exception(None, # <- type(e) by docs, but ignored
                                        ex, ex.__traceback__)
    for trace in traces:
        logger.error(trace)
Exemple #2
0
    def run(self, execution_context, _):
        """run the action"""
        text = execution_context.event.get('text', None)
        execution_context.finish(text)

        if text is not None:
            tts.say([text])
Exemple #3
0
def confirm(confirmation):
    from modulemanager import speech_event
    logging.debug("speech event >> clear(SpeechManager stop)")
    speech_event.clear()

    tts.say(confirmation)
    time.sleep(0.5)
    ans = listen_confirmation()
    if ans is None:
        tts.say("上手く聞こえませんでした,もう一度お願いします")
        ans = listen_confirmation()

    logging.debug("speech event >> set(SpeechManager resume)")
    speech_event.set()
    return ans
Exemple #4
0
    def execute(self, acts):
        for act in acts:
            if act["platform"] == "play_youtube":
                if "confirmation" in act:
                    phrases = [
                        '',
                        'わかりました,操作をキャンセルします',
                        '上手く聞こえませんでした'
                    ]
                    approved = confirm_and_post_result(act, self.user, self.ip, phrases)
                    if not approved:
                        return

                try:
                    Popen('pkill -9 mpv', shell=True)
                    time.sleep(0.3)
                    tts.say(act['data'] + "を検索します")
                    try:
                        youtube_result = Youtube(act['data'], result=1)
                        logging.debug('play: '+ youtube_result.url[1])
                        Popen("mpv '" + youtube_result.url[1] + "' --loop --no-video", shell=True)
                        # Popen("mpv '" + youtube_result.url[0] + "' --loop --no-video > /dev/null 2>&1", shell=True)
                    except:
                        traceback.print_exc()
                        tts.say(act['data'] + "は見つかりませんでした")
                        # Popen("mpv https://www.youtube.com/watch?v=HKKe7p44PDY --loop --no-video > /dev/null 2>&1", shell=True)
                except:
                    traceback.print_exc()

            if act["platform"] == "stop_youtube":
                if "confirmation" in act:
                    phrases = [
                        '音楽の再生を終了します',
                        'わかりました,操作をキャンセルします',
                        '上手く聞こえませんでした'
                    ]
                    approved = confirm_and_post_result(act, self.user, self.ip, phrases)
                    if not approved:
                        return

                try:
                    Popen('pkill -9 mpv', shell=True)
                except:
                    traceback.print_exc()
Exemple #5
0
    def execute(self, acts):
        for act in acts:
            print(">>", act['platform'])
            if "platform" in act and "data" in act:
                if act['platform'] == "irkit" and act['data'][0] == 'TV':
                    if 'confirmation' in act:
                        phrases = [
                            'テレビを操作します', 'わかりました,操作をキャンセルします', '上手く聞こえませんでした'
                        ]
                        approved = confirm_and_post_result(
                            act, self.user, self.ip, phrases)
                        if not approved:
                            return

                        irkit.post_messages(act['data'])
                    else:
                        irkit.post_messages(act['data'])

                elif act['platform'] == "tts":
                    tts.say(act['data'])
Exemple #6
0
    def run(self, execution_context, app_context):
        """run the action"""
        lang = app_context.get_config('behavior.news_react.lang')
        tweet_count = app_context.get_config('behavior.news_react.count')
        opening = app_context.get_config('behavior.news_react.opening')

        tts.say_random(opening)

        # get all tweets from user timeline
        tweets = twitter.get_statuses(tweet_count)

        # filter tweets by language
        tweets = [tweet for tweet in tweets if tweet['user']['lang'] == lang]

        if len(tweets) == 0:
            no_news_reacts = app_context.get_config(
                'behavior.news_react.no_data')
            tts.say_random_finish(no_news_reacts, execution_context)
            return

        # get random tweets
        rand_tweet = tweets[random.randint(0, len(tweets) - 1)]

        new_tmpl = app_context.get_config('behavior.news_react.templates')

        text = tts.get_random(new_tmpl, {
            'text': rand_tweet['text'],
            'user': rand_tweet['user']['name']
        })
        execution_context.finish(text)

        tts.say_random(app_context.get_config('behavior.news_react.has_data'))

        # filter URL & hashtags
        text = re.sub(r'http\S+', '', text)
        if not lang == 'en':
            text = re.sub(r'#\S+', '', text)

        tts.say([text])
Exemple #7
0
    def run(self, execution_context):
        # opening = app.get_config('behavior.entity_react.opening')
        no_data_react = self.get_config('behavior.entity_react.no_data')
        confused_react = self.get_config('behavior.entity_react.confused')
        not_support_react = self.get_config(
            'behavior.entity_react.not_support')

        inquire_type = execution_context.event_name.split('.')[2]
        tagged_text = execution_context.event.get('tagged_text')
        entity_name = [
            w[0] for w in tagged_text if w[1] == 'NN' or w[1] == 'JJ'
        ]

        file_names = get_file_names(entity_name)
        no_files = len(file_names)

        # not found
        if no_files == 0:
            tts.say_random_finish(no_data_react, execution_context)
            return

        # duplicates found
        if no_files > 1:
            entitie_names = [get_entity_name(f) for f in file_names]
            tts.say_random_finish(confused_react, execution_context, {
                'count': no_files,
                'entities': ', '.join(entitie_names)
            })
            return

        entity = get_entity(file_names[0])
        # not supported
        if inquire_type not in entity:
            tts.say_random_finish(not_support_react, execution_context)
            return
        info = entity[inquire_type]
        execution_context.finish(info)
        tts.say([info])
Exemple #8
0
def confirm_and_post_result(act, user, ip, phrases):
    ans = confirm(act['confirmation'])
    data_confirm = {
        'action': json.dumps(act),
        'user_name': user,
        'answer': ans
    }
    r = requests.post("%s/data/confirmation" % ip,
                      data=data_confirm,
                      verify=False)
    logging.debug(r)
    if ans is None:
        # answer is ambiguous
        tts.say(phrases[2])
    elif ans is False:
        # answer is negative
        tts.say(phrases[1])
    elif ans is True:
        # answer is positive
        tts.say(phrases[0])
    return ans
Exemple #9
0
 def read_long_text(self, text):
     """Read a possibly long text"""
     for line in text.splitlines():
         if not self.stopped and len(line) > 0:
             tts.say([line])
Exemple #10
0
    def run(self, execution_context):
        text = execution_context.event.get('text', None)
        execution_context.finish(text)

        if text is not None:
            tts.say([text])
Exemple #11
0
 def play(self, song):
     """Play a song by a TTS engine"""
     tts.say(song.get('lyrics'))
Exemple #12
0
def read_long_text(text):
    """Read a possibly long text"""
    for line in text.splitlines():
        if not FLAGS["stop"] and len(line) > 0:
            tts.say([line])
Exemple #13
0
def run(execution_context):
    """run the action"""
    text = execution_context.event.get('text', None)

    if text is not None:
        tts.say([text])
Exemple #14
0
 def run(self, execution_context, _):
     """run the action"""
     execution_context.finish(tts.LAST_SENTENCE)
     if tts.LAST_SENTENCE is not None:
         tts.say(tts.LAST_SENTENCE)
Exemple #15
0
    def speech(self, data, ans):
        if not self.listen_speech:
            if ans == 3:
                logging.debug("speech recognition: awake")
                tts.say("はい?")
                self.listen_speech = True
                self.listen_start = time.time()
                self.last_speech = time.time()
                self.speech_detected = False
            elif ans == 1:
                logging.debug("speech indication: yes")

                data = {
                    "user_name": self.user,
                    "time": time.time(),
                    "type": "yes"
                }

                try:
                    requests.post("%s/data/speech" % self.ip,
                                  data=data,
                                  verify=False)
                except:
                    logging.error("could not send speech event: yes")
            elif ans == 2:
                logging.debug("speech indication: no")

                data = {
                    "user_name": self.user,
                    "time": time.time(),
                    "type": "no"
                }

                try:
                    requests.post("%s/data/speech" % self.ip,
                                  data=data,
                                  verify=False,
                                  timeout=1)
                except:
                    logging.error("could not send speech event: no")
        elif ans == 0:
            self.last_speech = time.time()

        listen_duration = time.time() - self.listen_start
        start_delay = 0.5
        max_duration = 5

        if self.listen_speech and listen_duration > start_delay:
            speech_over = time.time() - self.last_speech > 1

            if not speech_over and speech_duration < max_duration:
                self.current_buffer.extend(data)

                if ans == 0:
                    self.speech_detected = True

            else:
                if len(self.current_buffer) > 0:
                    sampwidth = self.detector.audio.get_sample_size(
                        self.detector.audio.get_format_from_width(
                            self.detector.detector.BitsPerSample() / 8))

                    logging.debug("listening over")

                    if self.speech_detected:
                        try:
                            audiodata = sr.AudioData(
                                current_buffer, detector.detector.SampleRate(),
                                sampwidth)
                            text = self.recognizer.recognize_google(
                                audiodata, language="ja")
                            print("You said: " + text)

                            data = {
                                "user_name": self.user,
                                "time": time.time(),
                                "type": "speech",
                                "text": text
                            }
                            requests.post("%s/data/speech" % self.ip,
                                          data=data,
                                          verify=False)
                        except Exception as e:
                            tts.say("聞き取れませんでした")
                            print("some error")
                            print(e)
                    else:
                        logger.debug("no speech detected")
                        tts.say("何か言いましたか?")

                self.listen_speech = False
                self.current_buffer = bytearray(b'')
Exemple #16
0
 def run(self, execution_context):
     execution_context.finish(tts.LAST_SENTENCE)
     if tts.LAST_SENTENCE is not None:
         tts.say(tts.LAST_SENTENCE)