jgorset / facebook-messenger

Definitely the best way to make Bots on Facebook Messenger with Ruby
MIT License
962 stars 211 forks source link

how to start with wit.ai? #180

Closed mericda closed 6 years ago

mericda commented 7 years ago

Hi!

Thanks for developing this project! I am a ruby/sinatra newbie who is trying to develop a messenger bot, and this gem was very useful! I am now thinking about integrating wit.ai, but I am stuck with their example code, I know that plug-in supports built-in NLP = wit.ai, but couldn't manage to get started. I would appreciate any help! I can see from my console that I can get nlp confidence values etc. but I don't know how to work with them in my code.

Facebook's example code is:

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

function handleMessage(message) {
  // check greeting is here and is confident
  const greeting = firstEntity(message.nlp, 'greeting');
  if (greeting && greeting.confidence > 0.8) {
    sendResponse('Hi there!');
  } else { 
    // default logic
  }
}

my code is but I couldn't manage to get it work.

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!

mericda commented 7 years ago

edit: adjusted my code.

jgorset commented 6 years ago

Sorry for the late reply, @mericda. The reason this isn't working is because you're mixing two programming languages (JavaScript and Ruby). You'll have to learn one of them to write a bot, so I suggest you pick the one you like the most and read a book about it. :-)