{
  "input": "ajoute un anniversaire pour le treize novembre",
  "intent": {
    "intentName": "userName:addEvent",
    "probability": 0.9662896
  },
  "slots": [
    {
      "rawValue": "anniversaire",
      "value": {
        "kind": "Custom",
        "value": "anniversaire"
      },
      "range": {
        "start": 10,
        "end": 22
      },
      "entity": "Noms communs_fr",
      "slotName": "summary"
    },
    {
      "rawValue": "pour le treize novembre",
      "value": {
        "kind": "InstantTime",
        "value": "2019-11-13 00:00:00 +00:00",
        "grain": "Day",
        "precision": "Exact"
      },
      "range": {
        "start": 23,
        "end": 46
      },
      "entity": "snips/datetime",
      "slotName": "start_datetime"
    }
  ]
}

#!usr/bin/env python2
# coding: utf-8

from hermes_python.hermes import Hermes
import random

MQTT_HOST = "localhost"
MQTT_PORT = "1883"  # Standard MQTT port
MQTT_URL = "{}:{}".format(MQTT_HOST, MQTT_PORT)  # ==> "localhost:1883"

SNIPS_USER_NAME = "snips"
INTENT_STARTS_QUIZ = "{}:start_quiz".format(SNIPS_USER_NAME)  # ==> "snips:start_quiz"
INTENT_GIVES_ANSWER = "{}:gives_answer".format(SNIPS_USER_NAME)  # ==> "snips:gives_answer"
INTENT_DOESNT_KNOW = "{}:doesnt_know".format(SNIPS_USER_NAME)  # ==> "snips:doesnt_know"

sessions_states = {}

def starts_quiz(hermes, intent_message):
   session_id = intent_message.session_id

   # Generating question
   a = random.random() * 10 + 1
   b = random.random() * 10 + 1
   tts = "What's {} times {} ?".format(a, b)
   sessions_states[session_id] = [a, b]

   # Define what the assistant is able to understand
   intent_filter = [INTENT_DOESNT_KNOW, INTENT_GIVES_ANSWER]

   hermes.publish_continue_session(session_id, tts, intent_filter)

def gives_answer(hermes, intent_message):
   session_id = intent_message.session_id

   # get answer from user
   answer = intent_message.slot.get("answer")
   answer = None if answer is None else answer.first().value

   # defines  TTS
   tts_good = "Good answer !"
   tts_bad = "Wrong answer ..."
   tts_no_answer = "I'm sorry I didn't get your answer"
   tts = ""

   # Check user's answer
   if answer is None:
       tts = tts_no_answer
   elif answer == sessions_states[session_id][0] * sessions_states[session_id][1]:
       tts = tts_good
   else:
       tts = tts_bad

   hermes.publish_end_session(session_id, tts)

def doesnt_know(hermes, intent_message):
   session_id = intent_message.session_id
   tts = "Ok, at least try next time !"

   hermes.publish_end_session(session_id, tts)

def session_ended(hermes, session_ended_message):
   del sessions_states[session_ended_message.session_id]

with Hermes(MQTT_URL) as h:

   # Subscribe to intents
   h.subscribe_intent(INTENT_STARTS_QUIZ, starts_quiz)\
    .subscribe_intent(INTENT_GIVES_ANSWER, gives_answer)\
    .subscribe_intent(INTENT_DOESNT_KNOW, doesnt_know)

   # Subscribe endSession
   h.subscribe_session_ended(session_ended)

   # Start Listening
   h.loop_forever()

#!usr/bin/env python2
# coding: utf-8

from hermes_python.hermes import Hermes
import random

MQTT_HOST = "localhost"
MQTT_PORT = "1883"  # Standard MQTT port
MQTT_URL = "{}:{}".format(MQTT_HOST, MQTT_PORT)  # ==> "localhost:1883"

SNIPS_USER_NAME = "snips"
INTENT_STARTS_QUIZ = "{}:start_quiz".format(SNIPS_USER_NAME)  # ==> "snips:start_quiz"
INTENT_GIVES_ANSWER = "{}:gives_answer".format(SNIPS_USER_NAME)  # ==> "snips:gives_answer"
INTENT_DOESNT_KNOW = "{}:doesnt_know".format(SNIPS_USER_NAME)  # ==> "snips:doesnt_know"

sessions_states = {}

def starts_quiz(hermes, intent_message):
   session_id = intent_message.session_id

   # Generating question
   a = random.random() * 10 + 1
   b = random.random() * 10 + 1
   tts = "What's {} times {} ?".format(a, b)
   sessions_states[session_id] = [a, b]

   # Define what the assistant is able to understand
   intent_filter = [INTENT_DOESNT_KNOW, INTENT_GIVES_ANSWER]

   hermes.publish_continue_session(session_id, tts, intent_filter)

def gives_answer(hermes, intent_message):
   session_id = intent_message.session_id

   # get answer from user
   answer = intent_message.slot.get("answer")
   answer = None if answer is None else answer.first().value

   # defines  TTS
   tts_good = "Good answer !"
   tts_bad = "Wrong answer ..."
   tts_no_answer = "I'm sorry I didn't get your answer"
   tts = ""

   # Check user's answer
   if answer is None:
       tts = tts_no_answer
   elif answer == sessions_states[session_id][0] * sessions_states[session_id][1]:
       tts = tts_good
   else:
       tts = tts_bad

   hermes.publish_end_session(session_id, tts)

def doesnt_know(hermes, intent_message):
   session_id = intent_message.session_id
   tts = "Ok, at least try next time !"

   hermes.publish_end_session(session_id, tts)

def session_ended(hermes, session_ended_message):
   del sessions_states[session_ended_message.session_id]

with Hermes(MQTT_URL) as h:

   # Subscribe to intents
   h.subscribe_intent(INTENT_STARTS_QUIZ, starts_quiz)\
    .subscribe_intent(INTENT_GIVES_ANSWER, gives_answer)\
    .subscribe_intent(INTENT_DOESNT_KNOW, doesnt_know)

   # Subscribe endSession
   h.subscribe_session_ended(session_ended)

   # Start Listening
   h.loop_forever()