diff options
author | Zoffix Znet <cpan@zoffix.com> | 2016-07-29 19:21:20 -0400 |
---|---|---|
committer | Zoffix Znet <cpan@zoffix.com> | 2016-07-29 19:21:20 -0400 |
commit | e3c619494601a8628b8a27bd855fa9b1506c0728 (patch) | |
tree | 5bce316ec49ba996b69e2d150cf075fc3ec5bd65 /examples/03-github-notifications.p6 | |
parent | db4b16e06ee14205a988263942524e1d754f8f64 (diff) |
Add examples; fix up docs
Diffstat (limited to 'examples/03-github-notifications.p6')
-rw-r--r-- | examples/03-github-notifications.p6 | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/examples/03-github-notifications.p6 b/examples/03-github-notifications.p6 new file mode 100644 index 0000000..8dec6d2 --- /dev/null +++ b/examples/03-github-notifications.p6 @@ -0,0 +1,38 @@ +use lib <lib>; + +use IRC::Client; +use HTTP::Tinyish; +use JSON::Fast; + +class GitHub::Notifications does IRC::Client::Plugin { + has Str:D $.token = %*ENV<GITHUB_TOKEN>; + has $!ua = HTTP::Tinyish.new; + constant $API_URL = 'https://api.github.com/notifications'; + + method irc-connected ($) { + start react { + whenever self!notification.grep(* > 0) -> $num { + $.irc.send: :where<Zoffix> + :text("You have $num unread notifications!") + :notice; + } + } + } + + method !notification { + supply { + loop { + my $res = $!ua.get: $API_URL, :headers{ :Authorization("token $!token") }; + $res<success> and emit +grep *.<unread>, |from-json $res<content>; + sleep $res<headers><X-Poll-Interval> || 60; + } + } + } +} + +.run with IRC::Client.new: + :nick<MahBot> + :host<irc.freenode.net> + :channels<#zofbot> + :debug + :plugins(GitHub::Notifications.new) |