MSN ボット

30分プログラム、その234。Ruburpleを使ってMSNボットを作ってみる。
RuburpleはPhidgin(マルチプロトコルIM)のライブラリlibpurpleのRubyバインディング。ざっと使ってみたところ、全てのAPIが使えるわけではないっぽい。

MSN以外にもIRCとかSkypeとかにも対応しているから、いろんなBotが簡単に作れるんじゃね?とか思ったりしてる。

使い方

$ ruby msn.rb

ソースコード

#! /opt/local/bin/ruby -w
# -*- mode:ruby; coding:utf-8 -*-
#
# msn.rb - MNS echo bot
#
# Copyright(C) 2008 by mzp
# Author: MIZUNO Hiroki / mzpppp at gmail dot com
# http://howdyworld.org
#
# Timestamp: 2008/02/03 21:22:31
#
# This program is free software; you can redistribute it and/or
# modify it under MIT Lincence.
#

require 'ruburple'
Ruburple::init

module Ruburple
  def self.observe(event,&proc)
    self.subscribe(event,proc)
  end
end

Ruburple.observe(:received_im_msg){|account,address,msg,_|
  p account
  puts 'recv'
  account.connection.send_im address,msg
}

Ruburple.observe(:signed_on){|*args|
  puts 'sign on'
  p args
}

protocol = Ruburple::get_protocol 'MSN'
account = protocol.get_account 'username','password'
account.connect

trap(:INT){
  account.connection.close
  exit 1
}

loop{ sleep 1 }