Class: Lita::Handlers::AlexaNewsRecorder

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/alexa_news_recorder.rb

Instance Method Summary collapse

Instance Method Details

#alexa_response(message) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lita/handlers/alexa_news_recorder.rb', line 39

def alexa_response(message)
  {
    "version": "1.0",
    "sessionAttributes": {
    },
    "response": {
      "outputSpeech": {
        "type": "PlainText",
        "text": "Added your message to Lita's flash briefing: #{message}"
      },
      "card": {
        "type": "Simple",
        "title": "Recorded flash message",
        "content": "Added your message to Lita's flash briefing: #{message}"
      },
      "shouldEndSession": true
    }
  }
end

#check_for_publisher!Object

Raises:

  • (ConfigurationError)


25
26
27
28
# File 'lib/lita/handlers/alexa_news_recorder.rb', line 25

def check_for_publisher!
  return if robot.handlers.include?(Lita::Handlers::AlexaNewsPublisher)
  raise ConfigurationError, "Must install AlexaNewsPublisher handler to make me work!"
end

#extract_message(payload) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
# File 'lib/lita/handlers/alexa_news_recorder.rb', line 30

def extract_message(payload)
  parsed = JSON.parse(payload)

  value = parsed.dig('request', 'intent', 'slots', 'Message', 'value')

  raise ArgumentError if value.nil?
  value
end

#record_message(request, response) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lita/handlers/alexa_news_recorder.rb', line 9

def record_message(request, response)
  check_for_publisher!

  Lita.logger.debug "Inbound request params: "
  Lita.logger.debug request.params
  Lita.logger.debug "Inbound request body: "

  Lita.logger.debug request.body.string # it's a StringIO

  message = extract_message(request.body.string)

  robot.trigger(:save_alexa_message, username: 'Alexa News Recorder', message: message)

  response.write JSON.dump(alexa_response)
end