Bot::ChatBots::Messenger::Sender - Bot::ChatBots Sender for Messenger
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);
This is a sender for Facebook Messenger.
This class consumes the following roles (inheriting all its accessors):
Bot::ChatBots::Role::Sender
Bot::ChatBots::Role::UserAgent
The following additional accessors have a same-named option that can be passed to the constructor.
my $token = $obj->token;
Accessor for the token. This MUST be provided upon creation of the object.
my $fb_messenger_url = $obj->url;
The Facebook Messenger API endpoint. Defaults to https://graph.facebook.com/v2.6/me/messages.
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.
$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.
$message
message
If the recipient part is missing, it is possibly auto-filled based on the following algorithm:
recipient
if a $args{record} is provided, then the $args{record}{channel}{id} is taken from it;
$args{record}
$args{record}{channel}{id}
otherwise, if the "has_recipient" in Bot::ChatBots::Role::Sender method returns a true value, then "recipient" in Bot::ChatBots::Role::Sender is used;
otherwise an exception is thrown (Ouch with code 500).
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.
$args{callback}
Bot::ChatBots, Bot::ChatBots::Role::Sender and Bot::ChatBots::Role::UserAgent.
Flavio Poletti <polettix@cpan.org>
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.
To install Bot::ChatBots::Messenger, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Bot::ChatBots::Messenger
CPAN shell
perl -MCPAN -e shell install Bot::ChatBots::Messenger
For more information on module installation, please visit the detailed CPAN module installation guide.