20
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SkypeのチャットをリアルタイムにChatworkに転送するRubyスクリプト(更新:API利用版)

Last updated at Posted at 2013-11-19

Screen Shot 2013-11-20 at 12.23.19 AM.png

最終更新日

  • 2013-12-10

1行概要

Skypeの会話内容を準リアルタイム(遅延10秒)でChatworkの「マイチャット」に片方向転送するRubyのデーモンスクリプト。
ChatworkAPIを利用するように変更。

犯行動機

会社でskypeからchatworkへの移行をすすめるにあたって、その途中の「両方使ってる」微妙な段階の苦しみを取りぞぞくために。

覚書

  • 12/10時点でmac限定

コード

sk2ch.rb

require 'fileutils'
require 'sequel'
require 'faraday'


# Skypeのアカウント
SKYPE_NAME = 'skype.id'

# ChatworkのAPIトークン
CHATWORK_API_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'

# Chatworkに投稿するグループID(マイチャットなど)
CHATWORK_ROOM_ID = '12345678' # charwork my chat


module LastId

  DATA_PATH = File.join(File.expand_path(File.dirname(__FILE__)), 'last_id.txt')

  # 前回のSkypeのMessageIDを取得、なければSkypeDBから最新のものを取得
  def self.get
    if File.exist?(DATA_PATH)
      open(DATA_PATH).read
    else
      Skype::copy_db!
      Skype::get_last_id
    end
  end

  # ファイルにSkypeのMessageIDを保存
  def self.set(last_id)
    puts "last_id: #{last_id}"
    open(DATA_PATH, 'w') { |f| f.write last_id.to_s }
  end

end

module Skype

  TMP_DB_PATH = '/tmp/skype-main.db'

  # SkypeのDBをコピー
  def self.copy_db!
    FileUtils.copy("#{ENV["HOME"]}/Library/Application Support/Skype/#{SKYPE_NAME}/main.db", TMP_DB_PATH)
  rescue => err
    p err
    retry
  end

  # Skypeの最後のMessageIDを取得
  def self.get_last_id
    connection = Sequel.connect("sqlite://#{TMP_DB_PATH}")
    dataset = connection.fetch("SELECT id FROM Messages ORDER BY id DESC LIMIT 1")
    dataset.first[:id]
  end

  # Skypeの最近の更新の差分を取得
  def self.get_message

    last_message_id = LastId::get

    connection = Sequel.connect("sqlite://#{TMP_DB_PATH}")

    dataset = connection.fetch("SELECT Messages.id, Messages.from_dispname, Conversations.displayname, Messages.body_xml FROM Messages LEFT JOIN Conversations ON Messages.convo_id = Conversations.id WHERE Messages.id > ?", last_message_id)
    new_last_message_id = last_message_id
    dataset.each do |r|
      puts "[#{r[:from_dispname]}] #{r[:displayname]}\n\t#{r[:body_xml]}"
      yield r
      new_last_message_id = r[:id]
    end

    LastId::set(new_last_message_id)
  end

end

module Chatwork

  # ChatworkにPOST
  def self.post(text)

    conn = Faraday::Connection.new(url: 'https://api.chatwork.com') do |builder|
      builder.use Faraday::Request::UrlEncoded
      builder.use Faraday::Adapter::NetHttp
    end

    response = conn.post do |request|
      request.url "/v1/rooms/#{CHATWORK_ROOM_ID}/messages"
      request.headers = {
        'X-ChatWorkToken' => CHATWORK_API_TOKEN
      }
      request.params[:body] = text
    end

  end
end


loop {
  Skype::copy_db!
  Skype::get_message do |message|
    Chatwork::post("#{message[:displayname]} (#{message[:from_dispname]})\n#{message[:body_xml]}")

    sleep(1)
  end
  sleep(1)
}

細かい内容

  • SkypeのDB(sqlite)は排他制御で読み取りすら出来ないので、強引にコピーしSQLを実行して最新の情報を取得

参考

30秒でChatwork APIを使ってマイチャットに投稿する方法

20
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?