-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.pl
104 lines (90 loc) · 2.75 KB
/
bot.pl
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use strict;
use warnings;
use AnyEvent::XMPP::Client;
use AnyEvent::XMPP::Ext::Disco;
use AnyEvent::XMPP::Ext::Version;
use AnyEvent::XMPP::Ext::MUC;
use RT::Client::REST;
use DateTimeX::Easy;
use Data::Dumper::Perltidy;
$| = 1;
# USAGE
# !rt id=123
my $client = AnyEvent::XMPP::Client->new();
my $j = AnyEvent->condvar;
my $rturl;
my $rt_timezone; # timezone that is configured to be used by rt
my $timezone; # if rt box is in a differernt timezone than you need, set this param to desired tz
my $rt = RT::Client::REST->new(
server => $rturl, # 'http://rt.foo.com'
timeout => 30
);
my $disco = AnyEvent::XMPP::Ext::Disco->new;
my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco);
my $rt_user;
my $rt_pass;
my $rt_ticket = sub {
my $id = shift;
$rt->login(username => $rt_user, password => $rt_pass);
my $ticket = $rt->show(type => 'ticket', id => "$id");
return $ticket;
};
my $datemanip = sub {
my $date = shift;
$date = $date . " $rt_timezone"; # needs to be appended for conversion
my $dt = DateTimeX::Easy->parse($date);
$dt->set_time_zone($timezone);
return $dt->day_abbr . " " . $dt->month_abbr . " " . $dt->day . " " . $dt->hms;
};
my $mainreply = sub {
my ( $msg, $ticket ) = @_;
my $reply = $msg->make_reply;
my ( $queue, $subject, $owner, $created, $lastupdated ) = ( $ticket->{Queue}, $ticket->{Subject}, $ticket->{Owner}, $ticket->{Created}, $ticket->{LastUpdated} );
$created = $datemanip->($created);
$lastupdated = $datemanip->($lastupdated);
$reply->add_body("Ticket \"$subject\" in queue $queue, was created on $created, last updated on $lastupdated, and is owned by $owner\n");
$reply->send;
};
my $tmpreply = sub {
my $msg = shift;
my $reply = $msg->make_reply;
$reply->add_body("Getting data, please wait.");
$reply->send;
};
my $parsemsg = sub {
my $msg = shift;
my $body = $msg->body;
if ( $body =~ /^\!rt/ ) {
$tmpreply->($msg);
# get id, process
my ($id) = ( $body =~ /(?:^\!rt) id=(\d+)/ );
return $id;
}
};
my $jabber_user; # user@domain
my $jabber_pass;
my @jabber_rooms; # [email protected]
my $jabber_room_nick; # desired nick in the room
$client->add_extension($disco);
$client->add_extension($muc);
$client->add_account($jabber_user, $jabber_pass);
$client->reg_cb(
session_ready => sub {
my ( $cl, $acc ) = @_;
for my $room ( @jabber_rooms ) {
$muc->join_room($acc->connection, $room, $jabber_room_nick, { history => { chars => 0 } });
}
$muc->reg_cb(
message => sub {
my ( $cl, $acc, $msg, $is_echo ) = @_;
return if $is_echo;
return if $msg->is_delayed;
my $id = $parsemsg->($msg);
my $ticket = $rt_ticket->($id);
$mainreply->($msg, $ticket);
},
);
}
);
$client->start;
$j->wait;