The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Bot::ChatBots::Messenger::Sender - Bot::ChatBots Sender for Messenger

SYNOPSIS

   use Bot::ChatBots::Messenger::Sender;

   my $sender = Bot::ChatBots::Messenger::Sender->new(
      token => $ENV{TOKEN}, # mandatory!
   );

   my $specific_sender = Bot::ChatBots::Messenger::Sender->new(
      token => $ENV{TOKEN}, # mandatory!
      recipient => $ENV{RECIPIENT_ID},
   );

   $sender->send(
      {
         message   => {text => 'Aloha'},
         recipient => {id   => $some_id},
      }
   );

   # simple text suffices if there's a default recipient
   $specific_sender->send('Hello, how are you?');

   # well, just try to figure it out by yourself. Here `$input_record`
   # is some record received e.g. by Bot::ChatBots::Messenger::WebHook
   $sender->send('text message', $input_record);

DESCRIPTION

This is a sender for Facebook Messenger.

ACCESSORS

This class consumes the following roles (inheriting all its accessors):

The following additional accessors have a same-named option that can be passed to the constructor.

token

   my $token = $obj->token;

Accessor for the token. This MUST be provided upon creation of the object.

url

   my $fb_messenger_url = $obj->url;

The Facebook Messenger API endpoint. Defaults to https://graph.facebook.com/v2.6/me/messages.

METHODS

This class consumes the following roles (inheriting all its methods):

It should be safe to override the following methods in your classes composing this role.

send_message

   $obj->send_message($message); # OR
   $obj->send_message($message, %args); # OR
   $obj->send_message($message, \%args);

Do actual sending of a message. This method leverages "ua_request" in Bot::ChatBots::Role::Sender "ua_request" to do the actual network traffic.

A Messenger message is required to have the following structure:

   {
      message   => {text => $your_message},
      recipient => {id => $your_recipient_id},
   }

If the input $message is a hash reference, the message part is supposed to be OK.

If the recipient part is missing, it is possibly auto-filled based on the following algorithm:

You can also pass a $args{callback} sub reference. This will be passed over to the user agent, most probably the Mojo::UserAgent object that is provided by Bot::ChatBots::Role::Sender.

SEE ALSO

Bot::ChatBots, Bot::ChatBots::Role::Sender and Bot::ChatBots::Role::UserAgent.

AUTHOR

Flavio Poletti <polettix@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2016 by Flavio Poletti <polettix@cpan.org>

This module is free software. You can redistribute it and/or modify it under the terms of the Artistic License 2.0.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.