diff options
author | Matias Linares <matiaslina@openmailbox.org> | 2017-04-03 22:23:57 -0300 |
---|---|---|
committer | Matias Linares <matiaslina@openmailbox.org> | 2017-04-03 22:23:57 -0300 |
commit | 9c439e653e19472c242fc0bff10e31a57b1ea45f (patch) | |
tree | 36f247003cfbe8ec29f26c492d2c794e5d621e16 /examples | |
download | perl6-matrix-client-9c439e653e19472c242fc0bff10e31a57b1ea45f.tar.gz |
Initial commit
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/bot.p6 | 98 | ||||
-rw-r--r-- | examples/rooms.p6 | 6 |
2 files changed, 104 insertions, 0 deletions
diff --git a/examples/bot.p6 b/examples/bot.p6 new file mode 100755 index 0000000..cfc1247 --- /dev/null +++ b/examples/bot.p6 @@ -0,0 +1,98 @@ +#!/usr/bin/env perl6 +use v6; +use lib "lib"; +use JSON::Tiny; +use Matrix::Client; + +class Bot { + has $!name = "deprecated"; + has $!username is required; + has Bool $!register = False; + has @!room-ids; + + has $!on-event; + + has Matrix::Client $!client; + + submethod BUILD(:$username!, :$password!, :$home-server!, :@room-ids!, :$on-event!) { + $!client = Matrix::Client.new(:home-server($home-server)); + $!username = $username; + @!room-ids = @room-ids; + $!on-event = $on-event; + + $!client.login($!username, $password); + } + + method join-rooms() { + @!room-ids.map: { $!client.join-room($_) } + } + + method shutdown() { + $!client.finish; + } + + method listen() { + say "Listening"; + my $since = ""; + + loop { + my $sync = { room => timeline => limit => 1 }; + my $data = from-json($!client.sync(sync-filter => $sync, since => $since).content); + $since = $data<next_batch>; + + for $data<rooms><join>.kv -> $room-id, $d { + for @($d<timeline><events>) -> $ev { + if $ev<type> eq "m.room.message" { + if $ev<content><body>.match($!name) { + my $bot-msg = $!on-event($ev); + if so $bot-msg { + say "Sending message $bot-msg"; + my $res = $!client.send($room-id, ~$bot-msg); + if $res.is-success { + say $res.content; + } else { + warn $res.content; + die $res.status-line; + } + } + } + } + } + } + sleep(10); + } + } +} + +sub MAIN(Str:D $username, Str:D $password, :$home-server = "https://matrix.deprecated.org") { + my @rooms = "!bpHGYOiCGlvCZarfMH:matrix.deprecated.org"; + my $bot = Bot.new: + username => $username, + password => $password, + home-server => $home-server, + room-ids => @rooms, + on-event => -> $ev { + given $ev<content><body> { + when /"say hi"/ { + say "Someone is saying hi!"; + "Hello @ {DateTime.now}" + } + default { say "Dunno what's telling me"; Str } + } + }; + + signal(SIGINT).tap({ + $bot.shutdown; + exit 0; + }); + + my $ress = $bot.join-rooms; + for @($ress) -> $res { + if !$res.is-success { + warn $res.status-line; + warn $res.content; + } + } + + $bot.listen; +} diff --git a/examples/rooms.p6 b/examples/rooms.p6 new file mode 100644 index 0000000..7f62233 --- /dev/null +++ b/examples/rooms.p6 @@ -0,0 +1,6 @@ +use Matrix::Client; + +my $c = Matrix::Client.new: :home-server<https://matrix.deprecated.org>; +$c.login: @*ARGS[0], @*ARGS[1]; + +say $c.rooms; |