New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please, describe in documentation how to implement bots "main cycle" in async mode #15
Comments
Hi, first things first: when using I have written an example for you showing how to make a (very) basic async Telegram bot: #!/usr/bin/env perl
# Async Telegram bot implementation using WWW::Telegram::BotAPI
# See also https://gist.github.com/Robertof/d9358b80d95850e2eb34
use Mojo::Base -strict;
use WWW::Telegram::BotAPI;
my $api = WWW::Telegram::BotAPI->new (
token => (shift or die "ERROR: a token is required!\n"),
async => 1
);
# Increase the inactivity timeout of Mojo::UserAgent
$api->agent->inactivity_timeout (45);
# Fetch bot information asynchronously
my $me;
$api->getMe (wrap_async (sub {
$me = shift;
say "Received bot information from server: hello world, I'm $me->{first_name}!";
}));
# Async update handling
sub fetch_updates {
state $offset = 0;
$api->getUpdates ({
timeout => 30,
allowed_updates => ["message"], # remove me in production if you need multiple update types
$offset ? (offset => $offset) : ()
} => wrap_async (sub {
my $updates = shift;
for my $update (@$updates) {
$offset = $update->{update_id} + 1 if $update->{update_id} >= $offset;
# Handle text messages. No checks are needed because of `allowed_updates`
my $message_text = $update->{message}{text};
say "> Incoming message from \@$update->{message}{from}{username} at ",
scalar localtime;
say ">> $message_text";
# For this example, we're just going to handle messages containing '/say something'
if ($message_text =~ m!^/say (.+)!i) {
# For this example, we don't care about the result of sendMessage.
$api->sendMessage ({
chat_id => $update->{message}{chat}{id},
text => $1
} => sub { say "> Replied at ", scalar localtime })
}
}
# Run the request again using ourselves as the handler :-)
fetch_updates();
}));
};
fetch_updates();
Mojo::IOLoop->start;
sub wrap_async {
my $callback = shift;
sub {
my (undef, $tx) = @_;
my $response = $tx->res->json;
unless ($tx->success && $response && $response->{ok}) {
# TODO: if using this in production code, do NOT die on error, but handle them
# gracefully
say "ERROR: ", ($response->{error_code} ? "code $response->{error_code}: " : ""),
$response->{description} ?
$response->{description} :
($tx->error || {})->{message} || "something went wrong!";
Mojo::IOLoop->stop;
exit
}
$callback->($response->{result});
}
} Basically:
Hope it helps! Roberto |
Oh, thats great!
|
My monitoring system will be very glad to have possibility to annoy our duty in the Telegram group chat (after i've implement the bot based on ideas described in your example) :) |
Sorry, JSON::XS->new->pretty->encode($update) must be instead of JSON::XS->new->utf8->pretty->encode($update) (because of binmode on STDOUT) |
Your example works perfectly in conjuction with AnyEvent->timer handlers. |
Thank you for your valuable contribution! I appreciate your edits, and let me give some tips for you:
Thanks again and have a good day! |
I was trying to implement bot, which receives updates in async mode, but this is not work: code of my callback does not executed for some unknown reasons.
Code:
===
So it will be very helpful if you add some short and clear example of bots ""main loop" implementation in async mode.
Thank you!
The text was updated successfully, but these errors were encountered: