progapandist / rubotnik

"Bot-end" Ruby framework to quickly build and launch a Facebook Messenger chatbot
MIT License
189 stars 26 forks source link

Wit.ai Integration #3

Closed mericda closed 7 years ago

mericda commented 7 years ago

Hi @progapandist, I was trying to get Wit.ai work on rubotnik, but again locked up. Since I am not sure how to implement it through DSL route, I was calling facebook-messenger gem's own message.reply function since they already declared built-in nlp feature of Facebook. Through my server, heroku, I can see that wit.ai verifies and returns my calls true, but I can't think of any logic how to integrate it to the rubotnik. Any ideas you may have?

Right now, I am trying this code above the example DSL route reply code, but nothing happens. I don't probably approach this problem right way, but simply trying to get things together. I got this code from Facebook Built-in NLP feature doc.

Bot.on :message do |message|

  function firstEntity(nlp, name) {
    return nlp && nlp.entities && nlp.entities && nlp.entities[name] && nlp.entities[name][0];
  }

greetings = firstEntity(message.nlp, 'greetings')
  if greetings && greetings.confidence > 0.8
    message.reply(text: 'successful!')
  end

end

Thank you so much in advance!

mericda commented 7 years ago

edit: adjusted my code.

progapandist commented 7 years ago

Hi!

From what I see, you are mixing Ruby and JS in your example. Everything that facebook-messenger gives you, you can access through @message instance variable inside your command methods. Hope it helps!

mericda commented 7 years ago

I was finally able to find make it work!

Although it is not optimum, here is how I am handling an entity from Wit.AI. I am now trying to figure out how can I use Wit.AI boilerplate-wide such as replacing /keyword/i or bind 'keyword' helpers. Let me know if you have any ideas on how to integrate it.

  default do
        if text_message?
          entities = @message.nlp["entities"]
          keys = entities.keys
          # store the entity with the
          # highest confidence
          entity_max = nil
          confidence_max = 0
          # iterate over the keys and find
          #the one with the highest confidence
          keys.each do |key|
            confidence = entities[key].first['confidence']
            confidence = confidence.to_f
            puts "#{key} #{confidence}"
            if confidence > confidence_max
              entity_max = key
              confidence_max = confidence
            end
          end

            puts "Entity with max confidence: #{entity_max} #{confidence_max}"
            if entity_max == 'greetings' && confidence_max > 0.9
              say GREETINGS.sample + " 👋"
              UI::FBButtonTemplate.new(HELP_TEXT,HELP_BUTTONS).send(@user)
  end
        else
        end
      end
progapandist commented 7 years ago

Hi! I'm working on Wit.AI integration for a while, I'll release my first attempts some time before the end of the year. But the approach is very similar to what you described — your "router" can not know about parsed intents in advance, so you pipe all the messages through functions that recognise Wit.AI results. You're on a right track!