NAME

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

VERSION

version 0.008

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

  • a CODEREF to be executed to evaluate if we should respond to this message

  • a CODEREF to be executed if the previous criteria was true

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.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 218:

'=end' without a target?