stMind

about Tech, Computer vision and Machine learning

plaggerでTwitterのリプライをチェックしim.kayac.com経由でiPhoneに通知する

狐の王国 いまさらTwitter repliesなRSSがあることに気づいたのでPlaggerでチェックするようにした

確かにリプライに気づかないことがたまにあるので、チェックして知らせる仕組みがあれば、貴重なコミュニケーションの機会を逃さなくなるなぁと思い、上記を参考にして導入することにしました。
ただし、iPhoneに知らせるようにしたら、より早くリプライに気づけるので、im.kayac.comを経由してメッセージをPOSTするプラグインを書いて、リプライを通知することにしました。

作成したプラグインとconfig.yaml

プラグインの中身を簡単に解説します。
用意しているのは、initialize関数とnotify関数です。
config.yamlにusernameが指定されてないとPOSTすべきURLがわからないので、initialize関数でチェックして、指定されてなければエラーで終了するようにしています。
また、notify関数では、POSTの必須パラメータmessageだけはfeedのtitleと各エントリーのtitleから作り、その他のパラメータはconfig.yamlで指定された値を使用して、im.kayac.comにPOSTします。


Plugin/Notify/IphoneViaImKayac.pm

package Plagger::Plugin::Notify::IphoneViaImKayac;
use strict;
use base qw( Plagger::Plugin );

use LWP::UserAgent;
use HTTP::Request::Common;

sub register {
    my($self, $context) = @_;
    $context->register_hook(
	$self,
	'plugin.init' => \&initialize,
	'publish.feed' => \&notify,
	);
}

sub initialize {
    my($self, $context) = @_;

    $self->conf->{username} or Plagger::context->error("username is required");    
}

sub notify {
    my($self, $context, $args) = @_;

    return if $args->{feed}->count == 0;

    my $feed = $args->{feed};
    my $subject = $feed->title || '(no-title)';

    my $cfg = $self->conf;
    my $postto = 'http://im.kayac.com/api/post/' . $cfg->{username};
    $context->log(info => "Post these messages to $postto : $subject");

    my %postdata = ();
    
    my $all_msg = $subject;
    foreach my $entry ( $feed->entries ) {
	$all_msg = join("\n", $all_msg, $entry->title);
    }
    $postdata{message} = $all_msg;

    my $password = $cfg->{password} or '';
    if( $password ) { $postdata{password} = $password; }

    my $handler = $cfg->{handler} or '';
    if( $handler ) { $postdata{handler} = $handler; }

    my $sig = $cfg->{sig} or '';
    if( $sig ) { $postdata{sig} = $sig; }

    my $request = POST($postto, \%postdata);

    my $ua = LWP::UserAgent->new;
    my $res = $ua->request($request);

    unless ($res->is_success) {
	$context->error("Post failed : $res->content");
    } else {
	$context->log(info => $res->content);
    }
}

1;


続いて、config.yamlの中身ですが、usernameだけは必須で指定します。password、handler、sigはoptionalで指定出来ます。handlerは

handler: echofon://

とかしておくと、通知されたメッセージをタップすればアプリを起動できます。その他、指定可能なURLはここから辿れます。


config.yaml

global:
  assets_path: /your/assets_path
  cache:
    base: /your/cache

plugins:
  - module: UserAgent::AuthenRequest
    config:
      host: twitter.com:80
      auth: basic
      realm: Twitter API
      username: your username
      password: your password

  - module: Subscription::Config
    config:
      feed:
        - http://twitter.com/statuses/mentions.rss

  - module: Filter::Rule
    rule:
      module: Deduped
      path: /your/cache/your_cache.db

  - module: Notify::IphoneViaImKayac
    config:
      username: your username


プラグインは一応、githubで公開しております。
satojkovic's plagger at master - GitHub

最後に

今回、初めてプラグインを書いてみましたが、分からないなりに既存のものを参考にしながら書いた冗長な初心者プラグインでも、作法に従っていれば動かしてくれるplaggerは素晴らしい仕組みだなと思いました。

iPhoneへの通知が届くようになったのはいいのですが、バイブにしているので、一度だけブルっとなるだけで、電車乗ってるときとか歩いてるときは殆ど気づかないというのは大きな誤算でした。。。