jgorset / facebook-messenger

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

Pushing message to user without using message.reply #207

Closed elliottyson closed 6 years ago

elliottyson commented 6 years ago

Hi! I was a student @ Le Wagon coding bootcamp in Paris one month ago and we used your gem to create a bot as part of our 2-week project. Now I am improving the bot with new features and I am stuck with something which I think should be really simple for you guys. Actually I would like to know how to push a message to a user without using the message.reply script, that is to say just a push a message whenever I want. Maybe with Bot.deliver ? But to what id should I deliver? It does not work on my side. Many thanks for your help!

bstarr322 commented 6 years ago

Hi, I'm facing same issue. I generated page access token and passed webhook(messages, messaging_postbacks, messaging_optins, message_deliveries, message_reads) with this route(https://01741f12.ngrok.io/bot)

And the below code is what I configured:

# app/bot/bot.rb
require 'facebook/messenger'
include Facebook::Messenger
Bot.on :message do |message|
  message.reply(text: 'Hello, human!')
end
# config/initializers/bot.rb

unless Rails.env.production?
  bot_files = Dir[Rails.root.join('app', 'bot', 'bot.rb')]
  bot_reloader = ActiveSupport::FileUpdateChecker.new(bot_files) do
    bot_files.each{ |file| require_dependency file }
  end

  ActiveSupport::Reloader.to_prepare do
    bot_reloader.execute_if_updated
  end

  bot_files.each { |file| require_dependency file }
end
# .env
ACCESS_TOKEN=xxx
APP_SECRET=xxx
VERIFY_TOKEN=xxx
# config/application.rb
config.paths.add File.join('app', 'bot'), glob: File.join('bot.rb')
config.autoload_paths += Dir[Rails.root.join('app', 'bot', '*')]
# routes.rb
mount Facebook::Messenger::Server, at: 'bot'

I send a message 'Hi' to my app page but no reply.

I missed some config?

Thank you

amrutjadhav commented 6 years ago

@elliottyson You can't send message to users which are not subscribed to your bot. To send messages to someone on Messenger, the conversation must be initiated by the user. So you need user id i.e PSID to send messages. You app will receive the PSID in the webhook event's JSON when someone initiate interaction with your chabot.

elliottyson commented 6 years ago

@amrutjadhav yes I have the PSID of the users I want but I don't know how to push a message to a specific user without using the message.reply script, that is to say just a push a message whenever I want. Thanks

amrutjadhav commented 6 years ago

@elliottyson If you have PSID, then you can directly send like this.

payload = {
    recipient: { id: <PSID_OF_USER> },
    message: { text: <MESSAGE_YOU_WANT_TO_SEND> },
    message_type: <MESSAGE_TYPE>
  }
Facebook::Messenger::Bot.deliver(payload, access_token: <PAGE_ACCESS_TOKEN>)

If you not replying to user, you have to choose the proper message_type. You can find the available message_type here.

jgorset commented 6 years ago

🙌 Looks like @amrutjadhav solved this! I'll close.

elliottyson commented 6 years ago

Thanks @amrutjadhav it works now!