Bot-ChatBots-Telegram

 view release on metacpan or  search on metacpan

lib/Bot/ChatBots/Telegram/Guide/Tutorial.pod  view on Meta::CPAN

         elsif ($text eq '/hello') {
            $record->{send_response} = "Hello to you, $peer_name";
         }
         elsif ($text eq '/nag on') {
            nag_on($chat_id);
            $record->{send_response} = 'OK, to deactivate /nag off';
         }
         elsif ($text eq '/nag off') {
            nag_off($chat_id);
            $record->{send_response} = 'OK, to reactivate /nag on';
         }
         elsif (my (          $delay,        $msg) = $text =~ m{
               \A /remind \s+ ([1-9]\d*) \s+ (.*)
            }mxs)
         {
            Mojo::IOLoop->timer($delay => sub {
               $bcb->sender->send_message(
                  {
                     text => "$peer_name: remember $msg",
                     chat_id => $chat_id,
                  }
               );
            });
            $record->{send_response} = "I'll try my best!";
         }
      }

      return $record; # follow on..
   }


The program is a bit more complicated than before, but it does so much
more! Again, some notes:

=over

=item *

we are setting C<start> to C<0> when creating the object, instead of C<1>
as before. This will allow us to avoid starting the loop right on the
spot, define additional things (in our case, encapsulated inside
C<setup_recurring>) and then C<< $bcb->start >> the loop.

=item *

As anticipated, L<Mojo::IOLoop> is the real workhorse behind this, so we
can take advantage of its capabilities. Before C<start>ing, we set up
a recurrent job that will send a nagging message every 10 seconds to all
channels that ask for it (tracked through variable C<%nagged>).

=item *

The C<process_record> didn't change shape, just got a bit longer. The help
evolved to explain all the new commands, some of which are quite simple
(e.g. the nagging handling ones just set or reset a flag in C<%nagged>),
other again take advantage of L<Mojo::IOLoop> to do something (like the
new command C</remind>).

=item *

In both callbacks passed to L<Mojo::IOLoop> we use
a L<Bot::ChatBots::Telegram::Sender> object, relying upon the same object
that C<$bcb> uses to get new updates. As already anticipated, nothing

=item *

In all cases we send a quick feedback to the user, relying upon
C<send_message> as before. This will use C<$brb>'s internal
L<Bot::ChatBots::Telegram::Sender> instance to invoke the Telegram API;
stops you from creating another object using the same I<token>.

=back

=head2 Example Session

Start the new bot and try it:

   You   : /help
   TheBot: Very simple:
           * for help type /help
           * for greeting type /hello
           * for being annoyed every 10 s type /nag on
           * to stop annoyance type /nag off
           * to be reminded type /remind <seconds> <message>

Let's start some nagging, wait about 40 second before going on with other
commands:

   You   : /nag on
   TheBot: OK, to deactivate /nag off
   (some time after)
   TheBot: whooops!
   (10 s after)
   TheBot: whooops!
   (10 s after)
   TheBot: whooops!

You can send other commands in the meantime:

   TheBot: whooops!
   (10 s after)
   TheBot: whooops!
   You   : /hello
   TheBot: Hello to you, You
   (some time after)
   TheBot: whooops!
   (10 s after)
   TheBot: whooops!

Enough:

   TheBot: whooops!
   You   : /nag off
   TheBot: OK, to reactivate /nag on

Have to do something in 30 seconds?

   You   : /remind 30 do that thing!
   TheBot: I'll try my best!
   (30 seconds after)
   TheBot: You: remember do that thing!



( run in 0.853 second using v1.01-cache-2.11-cpan-39bf76dae61 )