Class: Pandorified::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/pandorified/session.rb

Overview

Represents a session (or conversation) for interacting with a bot.

Instance Method Summary collapse

Constructor Details

#initialize(botid, custid = nil) ⇒ Session

Note:

If you choose not to specify a custid, one will be automatically chosen and remembered throughout the session.

A new session for conversing with a bot.

Parameters:

  • botid (String)

    A valid Pandorabots botid.

  • custid (String) (defaults to: nil)

    An identifier used to keep track of this conversation.



19
20
21
22
# File 'lib/pandorified/session.rb', line 19

def initialize(botid, custid = nil)
  @botid = botid
  @custid = custid
end

Instance Method Details

#talk(input) ⇒ Pandorified::Result

Send a message to this session’s bot and receive a response.

See Result for how to check for an error response and get the error message.

Alternatively, you can use #talk! instead of this method, which raises an exception when Pandorabots API returns an error.

Parameters:

  • input (String)

    Text to say to the bot.

Returns:



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pandorified/session.rb', line 35

def talk(input)
  result = Pandorified::Result.new(
    botid: @botid,
    custid: @custid,
    input: input
  )

  @custid ||= result.custid if result.success?

  result
end

#talk!(input) ⇒ String

Send a message to this session’s bot and receive a response (if successful).

If Pandorabots API responds with an error, PandorabotsError is raised with the specific error message.

If you’d like to check for and handle the error yourself, you can use #talk instead of this method.

Parameters:

  • input (String)

    Text to say to the bot.

Returns:

  • (String)

    The bot’s response text.



60
61
62
63
64
65
66
67
68
69
# File 'lib/pandorified/session.rb', line 60

def talk!(input)
  result = talk(input)

  if result.error?
    msg = "Pandorabots returned status #{result.status}: #{result.message}"
    raise Pandorified::PandorabotsError, msg
  end

  result.that
end