| 1 | package Bot::Cobalt::Plugin::Twitter; | ||
| 2 | # ABSTRACT: Bot::Cobalt plugin for automatic tweeting | ||
| 3 | $Bot::Cobalt::Plugin::Twitter::VERSION = '0.003'; | ||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Bot::Cobalt; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Bot::Cobalt::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | use HTML::Entities qw(decode_entities); | ||
| 11 | use Mojo::UserAgent; | ||
| 12 | use Net::Twitter; | ||
| 13 | use Text::Unidecode qw(unidecode); | ||
| 14 | use URI::Find::Simple qw(list_uris); | ||
| 15 | |||
| 16 | my $status_rx = qr/twitter\.com\/\w+\/status\/(\d+)/; | ||
| 17 | |||
| 18 | sub new { bless {}, shift } | ||
| 19 | sub twitter { shift->{twitter} } | ||
| 20 | |||
| 21 | sub display_tweets { shift->{display_tweets} } | ||
| 22 | sub tweet_topics { shift->{tweet_topics} } | ||
| 23 | sub tweet_links { shift->{tweet_links} } | ||
| 24 | sub retweet_tweets { shift->{retweet_tweets} } | ||
| 25 | sub ua { shift->{useragent} } | ||
| 26 | |||
| 27 | sub Cobalt_register { | ||
| 28 | my $self = shift; | ||
| 29 | my $core = shift; | ||
| 30 | my $conf = $core->get_plugin_cfg($self); | ||
| 31 | |||
| 32 | $self->{display_tweets} = $conf->{display_tweets} // 1; | ||
| 33 | $self->{tweet_topics} = $conf->{tweet_topics} // 1; | ||
| 34 | $self->{tweet_links} = $conf->{tweet_links}; | ||
| 35 | $self->{retweet_tweets} = $conf->{retweet_tweets}; | ||
| 36 | $self->{useragent} = Mojo::UserAgent->new; | ||
| 37 | |||
| 38 | eval { | ||
| 39 | $self->{twitter} = Net::Twitter->new( | ||
| 40 | traits => [ qw(API::RESTv1_1 RetryOnError) ], | ||
| 41 | consumer_key => $conf->{consumer_key}, | ||
| 42 | consumer_secret => $conf->{consumer_secret}, | ||
| 43 | access_token => $conf->{access_token}, | ||
| 44 | access_token_secret => $conf->{access_token_secret}, | ||
| 45 | ssl => 1, | ||
| 46 | ); | ||
| 47 | }; | ||
| 48 | |||
| 49 | if (my $err = $@) { | ||
| 50 | logger->warn("Unable to create Net::Twitter object: $err"); | ||
| 51 | } | ||
| 52 | |||
| 53 | register( $self, 'SERVER', qw(public_msg public_cmd_tweet topic_changed) ); | ||
| 54 | logger->info("Registered, commands: !tweet"); | ||
| 55 | |||
| 56 | return PLUGIN_EAT_NONE; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub Cobalt_unregister { | ||
| 60 | my $self = shift; | ||
| 61 | my $core = shift; | ||
| 62 | |||
| 63 | logger->info("Unregistered"); | ||
| 64 | |||
| 65 | return PLUGIN_EAT_NONE; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub Bot_topic_changed { | ||
| 69 | my $self = shift; | ||
| 70 | my $core = shift; | ||
| 71 | my $topic = ${ shift() }; | ||
| 72 | |||
| 73 | return PLUGIN_EAT_NONE if not $self->tweet_topics; | ||
| 74 | |||
| 75 | my $new_topic = $topic->stripped; | ||
| 76 | my $status = substr($new_topic, 0, 139); | ||
| 77 | |||
| 78 | $self->twitter->update($status); | ||
| 79 | |||
| 80 | return PLUGIN_EAT_NONE; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub Bot_public_cmd_tweet { | ||
| 84 | my $self = shift; | ||
| 85 | my $core = shift; | ||
| 86 | my $msg = ${ shift() }; | ||
| 87 | |||
| 88 | my $context = $msg->context; | ||
| 89 | my $channel = $msg->target; | ||
| 90 | my $nick = $msg->src_nick; | ||
| 91 | |||
| 92 | my @split = split(/ /, $msg->stripped); | ||
| 93 | |||
| 94 | shift @split; # shift off the command | ||
| 95 | |||
| 96 | my $status = substr( join(' ', @split), 0, 139); | ||
| 97 | |||
| 98 | $self->twitter->update($status); | ||
| 99 | |||
| 100 | return PLUGIN_EAT_ALL; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub Bot_public_msg { | ||
| 104 | my $self = shift; | ||
| 105 | my $core = shift; | ||
| 106 | my $msg = ${ shift() }; | ||
| 107 | |||
| 108 | return PLUGIN_EAT_NONE | ||
| 109 | unless $self->display_tweets or $self->tweet_links or $self->retweet_tweets; | ||
| 110 | |||
| 111 | my $context = $msg->context; | ||
| 112 | my $channel = $msg->target; | ||
| 113 | |||
| 114 | foreach my $uri ( list_uris($msg->message) ) { | ||
| 115 | next if not $uri; | ||
| 116 | |||
| 117 | if ($uri =~ $status_rx) { | ||
| 118 | my $id = $1; | ||
| 119 | if ($self->retweet_tweets) { | ||
| 120 | eval { $self->twitter->retweet($id) }; | ||
| 121 | if (my $err = $@) { | ||
| 122 | logger->warn( | ||
| 123 | "Failed to retweet [$id] - " . $err->twitter_error_text, | ||
| 124 | ); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | if ($self->display_tweets) { | ||
| 129 | my $tweet = $self->twitter->show_status($id); | ||
| 130 | my $text = $tweet->{text}; | ||
| 131 | my $name = $tweet->{user}->{name}; | ||
| 132 | my $sname = $tweet->{user}->{screen_name}; | ||
| 133 | my $user = sprintf '%s (@%s)', $name, $sname; | ||
| 134 | |||
| 135 | $text = unidecode(decode_entities($text)); | ||
| 136 | my @lines = split /\n/, $text; | ||
| 137 | |||
| 138 | if (@lines == 1) { | ||
| 139 | broadcast( 'message', $context, $channel, "$user - $lines[0]" ); | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | broadcast( 'message', $context, $channel, $user ); | ||
| 143 | broadcast( 'message', $context, $channel, " - $_" ) | ||
| 144 | foreach @lines; | ||
| 145 | } | ||
| 146 | return PLUGIN_EAT_ALL; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | elsif ($self->tweet_links) { | ||
| 150 | my $title = $self->ua->get($uri)->result->dom->at('title')->text; | ||
| 151 | my $uni = unidecode($title) || $title; | ||
| 152 | my $short = substr($uni, 0, 100); | ||
| 153 | |||
| 154 | next if not $title; | ||
| 155 | |||
| 156 | # my $title = decode_entities( | ||
| 157 | # $ua->get($uri)->result->dom->at('title')->text | ||
| 158 | # ) or next; | ||
| 159 | |||
| 160 | if (length($short) < length($uni)) { | ||
| 161 | $short = "$short..."; | ||
| 162 | } | ||
| 163 | |||
| 164 | eval { $self->twitter->update("$short - $uri") }; | ||
| 165 | |||
| 166 | if (my $err = $@) { | ||
| 167 | logger->warn( | ||
| 168 | "Failed to tweet URL [$uri]: " . $err->twitter_error_text, | ||
| 169 | ); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | return PLUGIN_EAT_NONE; | ||
| 174 | } | ||
| 175 | |||
| 176 | 1; | ||
| 177 | |||
| 178 | __END__ | ||
| 179 | |||
| 180 | =pod | ||
| 181 | |||
| 182 | =encoding UTF-8 | ||
| 183 | |||
| 184 | =head1 NAME | ||
| 185 | |||
| 186 | Bot::Cobalt::Plugin::Twitter - Bot::Cobalt plugin for automatic tweeting | ||
| 187 | |||
| 188 | =head1 VERSION | ||
| 189 | |||
| 190 | version 0.003 | ||
| 191 | |||
| 192 | =head1 SYNOPSIS | ||
| 193 | |||
| 194 | ## In plugins.conf | ||
| 195 | Twitter: | ||
| 196 | Module: Bot::Cobalt::Plugin::Twitter | ||
| 197 | Config: plugins/twitter.conf | ||
| 198 | Opts: | ||
| 199 | display_tweets: 1 | ||
| 200 | retweet_tweets: 0 | ||
| 201 | tweet_links: 0 | ||
| 202 | tweet_topics: 1 | ||
| 203 | |||
| 204 | ## In plugins/twitter.conf | ||
| 205 | --- | ||
| 206 | consumer_key: <twitter consumer key> | ||
| 207 | consumer_secret: <twitter consumer secret> | ||
| 208 | access_token: <twitter access token> | ||
| 209 | access_token_secret: <twitter access token secret> | ||
| 210 | |||
| 211 | =head1 DESCRIPTION | ||
| 212 | |||
| 213 | A L<Bot::Cobalt> plugin. | ||
| 214 | |||
| 215 | This plugin will display the contents of a tweet that is linked in a channel. | ||
| 216 | Additionally, it does a handful of twitter-related functions. | ||
| 217 | |||
| 218 | =over 4 | ||
| 219 | |||
| 220 | =item display_tweets (default: on) | ||
| 221 | |||
| 222 | Whenever a twitter status link is said in chat, look it up and display it. | ||
| 223 | |||
| 224 | =item tweet_links (default: off) | ||
| 225 | |||
| 226 | Whenever a link is tweeted, tweet it (with title). | ||
| 227 | |||
| 228 | =item retweet_tweets (default: off) | ||
| 229 | |||
| 230 | Whenever a tweet is linked, retweet it. | ||
| 231 | |||
| 232 | =item tweet_topics (default: on) | ||
| 233 | |||
| 234 | Whenever a topic changes, tweet it. | ||
| 235 | |||
| 236 | =item !tweet | ||
| 237 | |||
| 238 | Finally a command, !tweet that will tweet the message you provide it. | ||
| 239 | |||
| 240 | =back | ||
| 241 | |||
| 242 | =head1 AUTHOR | ||
| 243 | |||
| 244 | Scott Miller <scott.j.miller@gmail.com> | ||
| 245 | |||
| 246 | =head1 COPYRIGHT AND LICENSE | ||
| 247 | |||
| 248 | This software is copyright (c) 2017 by Scott Miller. | ||
| 249 | |||
| 250 | This is free software; you can redistribute it and/or modify it under | ||
| 251 | the same terms as the Perl 5 programming language system itself. | ||
| 252 | |||
| 253 | =cut | ||