diff options
author | Matias Linares <matiaslina@gmail.com> | 2020-03-24 19:37:44 -0300 |
---|---|---|
committer | Matias Linares <matiaslina@gmail.com> | 2020-03-24 19:37:44 -0300 |
commit | e1abad7a57fd34ffb55c1d57d0b89ee91e55296b (patch) | |
tree | 12fe2d7b6d45543cb52f3b0382bc7de4bcc796fc /examples/bot.p6 | |
parent | 2fb9929b3acd2caf6ca0f0cf1e16f0872f7ae667 (diff) | |
parent | e50b8fa9e02b19ece80ebeb576051e451d3a7812 (diff) | |
download | perl6-matrix-client-e1abad7a57fd34ffb55c1d57d0b89ee91e55296b.tar.gz |
Merge branch 'master' into documentation
Diffstat (limited to 'examples/bot.p6')
-rwxr-xr-x | examples/bot.p6 | 81 |
1 files changed, 0 insertions, 81 deletions
diff --git a/examples/bot.p6 b/examples/bot.p6 deleted file mode 100755 index 24ab1e6..0000000 --- a/examples/bot.p6 +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env perl6 -use v6; -use lib "lib"; -use JSON::Tiny; -use Matrix::Client; -use Matrix::Client::Exception; - -class Bot { - has $!name = "!d"; - has $!username is required; - has Bool $!register = False; - - has $!on-event; - - has Matrix::Client $!client; - - submethod BUILD(:$username!, :$password!, :$home-server!,:$on-event!) { - $!client = Matrix::Client.new(:home-server($home-server)); - $!username = $username; - $!on-event = $on-event; - - $!client.login($!username, $password); - } - - method shutdown() { - $!client.save-auth-data; - } - - method listen() { - say "Listening"; - my $since = ""; - - loop { - my $sync = { room => timeline => limit => 1 }; - my $response = $!client.sync(sync-filter => $sync, since => $since); - $since = $response.next-batch; - - for $response.joined-rooms -> $room { - for $room.timeline - .events - .grep(*.type eq 'm.room.message') -> $msg { - if $msg.content<body>.match($!name) { - my $bot-msg = $!on-event($msg); - if so $bot-msg { - say "Sending message $bot-msg"; - try { - CATCH { when X::Matrix::Response { .message }} - my $res = $!client.send($room.room-id, ~$bot-msg); - } - } - } - } - } - sleep(10); - } - } -} - -sub MAIN(Str:D $username, Str:D $password, :$home-server = "https://matrix.deprecated.org") { - my $bot = Bot.new: - username => $username, - password => $password, - home-server => $home-server, - on-event => -> $ev { - given $ev.content<body> { - when /"say hi"/ { - say "Someone says {$ev.content<body>}"; - "Hello @ {DateTime.now}" - } - default { say "Dunno what's telling me"; Str } - } - }; - - signal(SIGINT).tap({ - say "Bye"; - $bot.shutdown; - exit 0; - }); - - $bot.listen; -} |