aboutsummaryrefslogtreecommitdiff
path: root/examples/bot.raku
blob: 8a9d2064470a6048576212ffdee611a94cb40db0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env perl6
use v6;
use lib "lib";
use JSON::Fast;
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;
}