NAME

Telegram::Bot::Brain - A base class to make your very own Telegram bot

VERSION

version 0.010

SYNOPSIS

package MyApp::Coolbot;

use Mojo::Base 'Telegram::Bot::Brain';

has token       => 'token-you-got-from-@botfather';

sub init {
    my $self = shift;
    $self->add_repeating_task(600, \&timed_task);
    $self->add_listener(\&criteria, \&response);
}

Elsewhere....

my $bot = MyApp::Coolbot->new();
$bot->think;  # this will block unless there is already an event
              # loop running

DESCRIPTION

This base class makes it easy to create your own Bot classes that interface with the Telegram Bot API.

Internally it uses the Mojo::IOLoop event loop to provide non-blocking access to the Bot API, allowing your bot to listen for events via the longpoll getUpdates API method and also trigger timed events that can run without blocking.

METHODS

add_repeating_task

This method will add a sub to run every $seconds seconds.

add_listener

Respond to messages we receive. It takes two arguments

  • CODEREF or regular expression

    The coderef should return a true or false value, based on the input message. It is called as an object method on your subclass, with the first argument being the message object. If you instead supply a regular expression, the message object's text component is checked against it.

  • CODEREF to be executed if the previous criteria was true

    As above, it is called as an object method, and the first argument is the message object that you are responding to.

  • an optional hashref of arguments

Each CODEREF is passed two arguments, this Telegram::Bot::Brain object, and the Telegram::Bot::Message object, the message that was sent to us.

send_to_chat_id

Send a pre-constructed message (some subclass of Telegram::Bot::Message) to a chat id.

send_message_to_chat_id

Send a plain text message to a chat_id (group or individual).

Can be passed an optional hashref which is passed directly to the Telegram Bot API, for extra arguments like parse_mode and so on.

$self->send_message_to_chat_id($msg->chat->id, "<pre>$text</pre>", { parse_mode => 'HTML' });

SEE ALSO

AUTHOR

Justin Hawkins <justin@eatmorecode.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Justin Hawkins.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.