Skip to content

The Ruby library for Facebook Messenger's Bot API - from Robochy

License

Notifications You must be signed in to change notification settings

nimakaviani/fabychy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rubychy

fabychy

A ruby client for Facebook Messenger's bot API provided by Robochy.

Installation

Add the gem to your application's Gemfile

gem 'fabychy'

and run the following

$ bundle install

Usage

Create a bot using Facebook Messenger developer portal. Once the registration is complete, copy the Page Access Token to be used by this library.

With your Page Access Token you can use fabychy like the following:

require 'fabychy'

bot = Fabychy::Bot.new('[PAGE ACCESS TOKEN]')

Get User Information

You need to have the USER_ID for the target user and then call the get_user function:

require 'fabychy'

bot = Fabychy::Bot.new('[PAGE ACCESS TOKEN]')
bot.get_user('[USER_ID]')

Sending Messages

With a created bot you can create messages of different types, attach custom keyboards, and pass them to the send_message function as follows:

require 'fabychy'

bot = Fabychy::Bot.new('[PAGE ACCESS TOKEN]')
message = Fabychy::Message.new(
  recipient: {id: [USER_ID] },
  message: {
    text: "hi there"
  },
  notification_type: Fabychy::Notification::REGULAR
)

The following notification types can be used:

Fabychy::Notification::REGULAR
Fabychy::Notification::SILENT_PUSH
Fabychy::Notification::NO_PUSH

DataTypes and Validations

Fabychy provides some light-weight validation of the generated message. However, when there is too much scaffolding it also allows you to pass the hash object instead of an actual element to create your messages. Below is an example:

require 'fabychy/message'

bot = Fabychy::Bot.new('[PAGE ACCESS TOKEN]')
message = Fabychy::Message.new(
  recipient: {id: "1056613714384965"},
  message: Fabychy::DataTypes::MessageBody.new(
    attachment: Fabychy::DataTypes::TemplateAttachment.new(
        payload: Fabychy::DataTypes::GenericTemplatePayload.new(
          elements: [
            Fabychy::DataTypes::GenericElement.new(
                title: 123,
                image_url: "http://wip.org/media/nima-456.jpg",
                subtitle: "This thing is great!",
                buttons: [
                  Fabychy::DataTypes::Button.new(
                    type: "web_url",
                    title: "robochy",
                    url: "http://robochy.com"
                  )
                ]
              ),
            Fabychy::DataTypes::GenericElement.new(
                title: "hello",
                image_url: "http://wip.org/media/nima-456.jpg",
                subtitle: "This thing a second thing great!",
              )
          ]
        )
      )
    )
  )
  bot.send_message(message)

Allowed Object Types

Attachments

Fabychy::DataTypes::TemplateAttachment
# OR
Fabychy::DataTypes::ImageAttachment

Payloads

# use the following for Fabychy::DataTypes::ImageAttachment
Fabychy::DataTypes::ImagePayload 

# use the following for Fabychy::DataTypes::TemplateAttachment
Fabychy::DataTypes::GenericTemplatePayload 
Fabychy::DataTypes::ButtonTemplatePayload 
Fabychy::DataTypes::ReceiptTemplatePayload 

Elements

# for Fabychy::DataTypes::GenericTemplatePayload 
Fabychy::DataTypes::GenericElement

# for Fabychy::DataTypes::ReceiptTemplatePayload 
Fabychy::DataTypes::ReceiptElement

Parsing the Response

In your callback servlet, pass the received request (not the request body) to the Fabychy::ApiResponse.parse function and you will get the hash response back:

class Simple < WEBrick::HTTPServlet::AbstractServlet
  def do_POST(request, response)
    response = Fabychy::ApiResponse.parse(request) # response is of type Hash
  end
end

Contributing

  • Fork it: https://github.com/nkaviani/fabychy/fork
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create a new Pull Request

About

The Ruby library for Facebook Messenger's Bot API - from Robochy

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages