1POE::Component::IRC::CoUoskebrooCko:n:tTrriabnuPstOleEad:t:oPCreo(rm3lp)oDnoecnutm:e:nItRaCt:i:oCnookbook::Translator(3)
2
3
4
6 POE::Component::IRC::Cookbook::Translator - A bot that can translate
7 text
8
10 This bot uses POE::Component::Lingua::Translate to translate text for
11 channel members. It makes use of the "BotCommand" plugin to handle the
12 translate command.
13
15 #!/usr/bin/env perl
16
17 use strict;
18 use warnings;
19 use Encode qw(decode);
20 use Encode::Guess;
21 use IRC::Utils qw(decode_irc parse_user);
22 use POE;
23 use POE::Component::IRC::State;
24 use POE::Component::IRC::Plugin::AutoJoin;
25 use POE::Component::IRC::Plugin::BotCommand;
26 use POE::Component::Lingua::Translate;
27
28 POE::Session->create(
29 package_states => [
30 main => [ qw(_start irc_botcmd_trans translated) ]
31 ],
32 heap => {
33 translators => { },
34 }
35 );
36
37 $poe_kernel->run();
38
39 sub _start {
40 my $heap = $_[HEAP];
41 my $irc = POE::Component::IRC::State->spawn(
42 Nick => 'translator_bot',
43 Server => 'irc.freenode.net',
44 );
45 $heap->{irc} = $irc;
46
47 $irc->plugin_add('AutoJoin', POE::Component::IRC::Plugin::AutoJoin->new(
48 Channels => [ '#test_channel1', '#test_channel2' ]
49 ));
50
51 $irc->plugin_add('BotCommand', POE::Component::IRC::Plugin::BotCommand->new(
52 Commands => {
53 trans => 'Usage: trans <from>,<to> <text>'
54 }
55 ));
56
57 $irc->yield(register => 'botcmd_trans');
58 $irc->yield('connect');
59 return;
60 }
61
62 sub irc_botcmd_trans {
63 my $heap = $_[HEAP];
64 my $irc = $heap->{irc};
65 my $nick = parse_user( $_[ARG0] );
66 my $channel = $_[ARG1];
67 my ($from, $to, $text) = split /,|\s+/, $_[ARG2], 3;
68
69 if (!exists $heap->{translators}->{$from . $to}) {
70 eval {
71 $heap->{translators}->{$from . $to} = POE::Component::Lingua::Translate->new(
72 alias => $from . $to,
73 back_end => 'Babelfish',
74 src => $from,
75 dest => $to,
76 );
77 };
78
79 if ($@) {
80 $irc->yield(privmsg => $channel, "$nick: There was an error: $@");
81 return;
82 }
83 }
84
85 $poe_kernel->post($from . $to => translate =>
86 decode_irc($text),
87 {
88 channel => $channel,
89 nick => $nick,
90 }
91 );
92 return;
93 }
94
95 sub translated {
96 my $irc = $_[HEAP]->{irc};
97 my ($text, $context, $error) = @_[ARG0, ARG1, ARG2];
98
99 if ($error) {
100 $irc->yield(
101 'privmsg',
102 $context->{channel},
103 $context->{nick} . ": There was an error: $error",
104 );
105 return;
106 }
107
108 $irc->yield(
109 'privmsg',
110 $context->{channel},
111 $context->{nick} . ': ' . $text,
112 );
113 return;
114 }
115
117 Hinrik Örn Sigurðsson, hinrik.sig@gmail.com
118
119
120
121perl v5.32.0 2P0O2E0:-:0C7o-m2p8onent::IRC::Cookbook::Translator(3)