adhearsion / adhearsion-asr

Adds speech recognition support to Adhearsion as a plugin.
http://adhearsion.com
MIT License
6 stars 6 forks source link

Menu Doesn't Seem to Set Proper Grammar #17

Closed vindir closed 10 years ago

vindir commented 10 years ago

Not sure of cause to narrow it down.

Attempting to set a matcher from an instance variable for a PIN code.

gist: https://gist.github.com/vindir/d46e4fe75889043a8703

Code Snippet:

  def run
    @pin = metadata[:pin]
    authenticate_customer
     ...
  end

  def authenticate_customer
    menu t('94'), {timeout: 20.seconds, tries: 2} do
      match(0) do
        dial_customer_care
      end
      match(@pin) do
        set_success
      end
  end

Resulting Grammar:

13:09:07 ahn.1  | [2014-02-24 13:09:07.066] TRACE Punchblock::Connection::XMPP: SENDING: (/Users/jowens/Workspace/pacificmez/repos/psychic_elements_telephony/ahn/vendor/ruby/ruby/2.1.0/gems/blather-1.0.0/lib/blather/client/client.rb:149:in `write') <iq type="set" to="da43ddfd-8b46-4b1f-88ff-1895caa8b643@psychic-elements-telephony.local" id="97924495-6ccc-487b-99d1-5945ef4a6868">
13:09:07 ahn.1  |   <prompt xmlns="urn:xmpp:rayo:prompt:1" barge-in="true">
13:09:07 ahn.1  |     <input xmlns="urn:xmpp:rayo:input:1" max-silence="20000" min-confidence="0.5" mode="dtmf" language="en-US" initial-timeout="20000" inter-digit-timeout="20000">
13:09:07 ahn.1  |       <grammar content-type="application/srgs+xml"><![CDATA[<grammar xmlns="http://www.w3.org/2001/06/grammar" version="1.0" xml:lang="en-US" mode="dtmf" root="options" tag-format="semantics/1.0-literals">
13:09:07 ahn.1  |   <rule id="options" scope="public">
13:09:07 ahn.1  |     <item>
13:09:07 ahn.1  |       <one-of>
13:09:07 ahn.1  |         <item><tag>0</tag>0</item>
13:09:07 ahn.1  |         <item>
13:09:07 ahn.1  |           <tag>1</tag>
13:09:07 ahn.1  |         </item>
13:09:07 ahn.1  |       </one-of>
13:09:07 ahn.1  |     </item>
13:09:07 ahn.1  |   </rule>
13:09:07 ahn.1  | </grammar>]]></grammar>
benlangfeld commented 10 years ago

Ah, so unfortunately you can't do this. The context within the menu block is a menu instance, hence why you can call #match seemingly on nothing. This means that the instance variable you're referencing, @pin, is being looked up on the MenuBuilder itself, and thus comes out as nothing at all.

My preferred solution is this:

class MyController < Adhearsion::CallController
  def run
    @pin = metadata[:pin]
    menu t('94'), timeout: 20.seconds, tries: 2 do
      match(0) do
        dial_customer_care
      end

      match(pin) do
        set_success
      end
    end
  end

private

  def pin
    @pin
  end
end

or if you don't really need the assignment:

class MyController < Adhearsion::CallController
  def run
    menu t('94'), timeout: 20.seconds, tries: 2 do
      match(0) do
        dial_customer_care
      end

      match(metadata[:pin]) do
        set_success
      end
    end
  end
end

That hash lookup is gonna be super-fast anyway, so probably not worth caching the PIN in an instance variable unless you have other cause to do so.