diff --git a/TODO b/TODO index 7ce72ac..9cf7528 100644 --- a/TODO +++ b/TODO @@ -2,8 +2,6 @@ * weather grab * currency grab * joyreactor video grab/stream -* jabber client (send message on demand) -* irc client (send message on demand) * paste/link shortener * shortener api for goo.gl or same services * paste api for some pastebins @@ -11,3 +9,7 @@ * repo uploader * repo metadata re-generator * simple monitoring api (common metrics: cpu, mem, swap, la, etc) +* jabber bot: send memos to offline participants when they return +* announce youtube video title +* common bot functions move to separate module +* bot bridge telegram-irc-jabber \ No newline at end of file diff --git a/cpanfile b/cpanfile index e5d0c90..76bf534 100644 --- a/cpanfile +++ b/cpanfile @@ -1,8 +1,11 @@ requires 'local::lib'; -requires 'Plack'; -requires 'Net::Jabber::Bot'; -requires 'Net::IRC'; -requires 'Net::LibIDN'; requires 'HTTP::Tiny'; -requires 'IO::Socket::SSL'; requires 'Hailo'; +requires 'IO::Socket::SSL'; +requires 'Image::Magick'; +requires 'Net::IRC'; +requires 'Net::Jabber::Bot'; +requires 'Net::LibIDN'; +requires 'Plack'; +requires 'Telegram::Bot'; +requires 'URI::URL'; diff --git a/data/myapi.json.sample b/data/myapi.json.sample index 11c63bb..452aa07 100644 --- a/data/myapi.json.sample +++ b/data/myapi.json.sample @@ -11,5 +11,35 @@ "mydir" : "/data/upload", }, "auth": "iliveoncemore" + }, + "jabberbot": { + "bot1" : { + "state" : "enabled", + "host" : "localhost", + "confsrv": "conference.localhost", + "room" : "myroom", + "authname" : "botuser", + "mucname" : "Buldozer", + "password": "SECRET", + "brain": "./data/buldozer.jabber.sqlite", + "csign": "!" + }, + "instances": [ "bot1" ] + }, + "ircbot" : { + "bot1" : { + "state": "enabled", + "brain": "data/threadripper.irc.sqlite", + "name" : "bot2", + "host" : "localhost", + "port" : 6667, + "channel" : "#mychan", + "csign" : "!" + } + }, + "telegrambot" : { + "name" : "gozerbot", + "token" : "botfather:token", + "brain": "./data/gozerbot.telegram.sqlite" } } diff --git a/lib/ircbot.pm b/lib/ircbot.pm new file mode 100644 index 0000000..7e3feed --- /dev/null +++ b/lib/ircbot.pm @@ -0,0 +1,88 @@ +package ircbot; + +use strict; +use warnings "all"; +use diagnostics; +use vars qw/$VERSION/; +use Net::IRC; +use v5.10.0; +use Moose; +use Hailo; +use Encode; +use Data::Dumper; + +#use lib qw(./lib ./vendor_perl); +use conf; + +use Exporter qw(import); +our @EXPORT = qw(run_ircbot); + +$VERSION = "1.0"; + +my $c = loadConf(); +my @hailo; +my $hailo; + +sub on_connect { + my $conn = shift; + $conn->join($conn->{channel}); + sleep 1; +# $conn->privmsg($conn->{channel}, 'Hello everyone!'); + $conn->{connected} = 1; +} + +sub on_msg { + my ($conn, $event) = @_; + $conn->privmsg($event->{nick}, "Get lost."); +} + +sub on_public { + my ($conn, $event) = @_; + my $text = decode('utf-8', $event->{args}[0]); + my $instance = 'bot1'; + say Dumper($conn); + say Dumper($event); + + if ($text =~ /^\!$conn->{_nick} (.+)/) { + # if so, pass the text and the nick off to the weather method + my $answer = $hailo->learn_reply($1); + $answer = encode('utf-8', $answer); + my @texts = split("\n", $answer); + + foreach (@texts) { + $conn->privmsg($event->{to}[0], $_); + } + } else { + $hailo->learn($text); + } +} + + +sub __run_ircbot_istance($) { + my $instance = shift; + + $hailo = Hailo->new( + brain => $c->{ircbot}->{$instance}->{brain}, + order => 3 + ); + + my $irc = new Net::IRC; + my $conn = $irc->newconn( + Nick => $c->{ircbot}->{$instance}->{name}, + Server => $c->{ircbot}->{$instance}->{host}, + Port => $c->{ircbot}->{$instance}->{port}, + Ircname => 'myapi irc module' + ); + + $conn->{channel} = $c->{ircbot}->{$instance}->{channel}; + $conn->add_handler('376', \&on_connect); + $conn->add_handler('msg', \&on_msg); + $conn->add_handler('public', \&on_public); + $irc->start(); +} + +sub run_ircbot { + __run_ircbot_istance('bot1'); +} + +1; diff --git a/lib/telegrambot.pm b/lib/telegrambot.pm new file mode 100644 index 0000000..9589ce1 --- /dev/null +++ b/lib/telegrambot.pm @@ -0,0 +1,65 @@ +package telegrambot; + +use strict; +use warnings "all"; +use diagnostics; +use vars qw/$VERSION/; +use v5.10.0; +use Hailo; +use Encode; +use Data::Dumper; +use Mojo::Base 'Telegram::Bot::Brain'; + +use conf; + +use Exporter qw(import); +our @EXPORT = qw(run_telegrambot); + +$VERSION = "1.0"; + +my $c = loadConf(); +my $hailo; + +has token => $c->{telegrambot}->{token}; + +sub __on_msg { + my ($self, $msg) = @_; + my $text = encode('utf-8', $msg->text); + +# is this a 1-on-1 ? + if ($msg->chat->is_user) { + $msg->reply("Я вас не знаю, идите лесом."); + +# group chat + } else { + my $qname = quotemeta($c->{telegrambot}->{name}); + return unless $text =~ /^$qname[\,|\:]? (.+)/; + my $reply = $hailo->learn_reply(decode('utf-8', $1)); + + if (defined($reply) && $reply ne '') { + $msg->reply($reply); + } + } +} + +# setup our bot +sub init { + $hailo = Hailo->new( + brain => $c->{telegrambot}->{brain}, + order => 3 + ); + my $self = shift; + $self->add_listener(\&__on_msg); +} + +sub run_telegrambot { + while (sleep 3) { + eval { + telegrambot->new->think; + } + } +} + +1; + +# vim: ft=perl noet ai ts=4 sw=4 sts=4: