46
Copyright(c) Yusuke Yamamoto All rights reserved. エンタープライズアプリケーション Twitterの連携 2011/7/28 Twitter4J.org 山本 裕介 1

エンタープライズアプリケーション とTwitterの連携

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

エンタープライズアプリケーションとTwitterの連携

2011/7/28

Twitter4J.org 山本 裕介

1

Page 2: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

山本 裕介http://twitter4j.org/

http://samuraism.jp/@yusukey

2

Page 3: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

話す内容• Twitter概要

• Twitter API

• 企業におけるTwitterの利用方法

• JavaEEとTwitter

• アプリケーション開発のコツ

3

Page 4: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter概要ブログとTwitterの比較

4

Page 5: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

• Twitter

‣ 140文字のみ

本文

タイトル本文

位置情報などのメタ情報を付けることも可能

記事の構成• ブログ‣ 自由な記述

5

Page 6: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

記事の配信• ブログ‣ プル型の情報配信‣ ブックマークして巡回‣ 更新していないと訪問してもらえなくなる

‣ RSSリーダーで購読

‣ 疑似プッシュ型

‣ 情報リテラシの高い読者に限る

• Twitter

‣ プル/プッシュ両方可能

‣ twitter.com より(プル)

‣ 専用アプリより(プッシュ)

ユーザーの約7割がアプリ利用

6

ほとんどのユーザーにプッシュ配信できる

Page 7: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

API• Twitter

‣ REST APIによる更新

‣ 記事の投稿、検索、取得等豊富

• ブログ‣ XML-RPC(Blogger API)

‣ 互換性に一部問題‣ 記事の投稿・修正が可能

7

Page 8: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

コミュニケーション• Twitter

‣ 対照ツイート(140文字)とツイート(140文字)

‣ コメント認証‣ 統一されたアカウント‣ CAPTHA不要

• ブログ‣ 非対照記事(フリースタイル)とコメント(テキストのみ)

‣ コメント認証‣ 個別のアカウント

(TypePad,Blogger等)

‣ CAPTHAが必要

8

Page 9: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter• デファクトスタンダード• 多くのユーザーを抱える‣ 既にmixiよりもユーザーが多いとの集計も

• 容易な双方向のコミュニケーション• 盛んなAPI利用

9

Page 10: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter API

10

Page 11: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter APIの種類• REST API

• Search API

• Streaming API

• Web サイト向けAPI

11

Page 12: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

REST API• REST: REpresentational State Transfer

• HTTPでAPI呼び出しGET /1/statuses/public_timeline.json HTTP/1.1Host: api.twitter.com

HTTP/1.1 200 OKDate: Tue, 08 Feb 2011 07:52:51 GMTServer: hiStatus: 200 OKContent-Type: application/json; charset=utf-8Vary: Accept-EncodingConnection: close

[{"source": "<a href=\"http://foursquare.com\" rel=\"nofollow\">foursquare<\/a>", "text": "I'm at IKEA 新三郷店 (新三郷ららシティ2-2-2, 三郷市) [pic]: http://4sq.com/eOHZdS", ... "user": { "description": "Javaエンジニア山本裕介。\r\n映画、アーチェリー、Java 好き\r\n#twitter4j #samurai", "name": "山本 裕介", ...} }, ...]

リクエスト

レスポンスヘッダ

レスポンスボディ

12

Page 13: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter のREST API• プル型‣ タイムラインの取得‣ ユーザーのフォロー‣ ツイート‣ ダイレクトメッセージの送受信

等々100近くのメソッド

13

Page 14: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Search API• プル型‣ 過去のツイートを検索• search.twitter.comとほぼ同じ機能

14

Page 15: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Streaming API• プッシュ型• 特定のユーザーのアクティビティをリアルタイムに取得

• 指定したキーワードにマッチするツイートをリアルタイムに取得

15

Page 16: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Webサイト向けAPIサーバサイドの実装不要

• Web Intents

‣ 簡単なリンクを張るだけでTwitterと連携

‣ ツイート、お気に入り追加、フォロー等

• @Anywhere

‣ JavaScriptでTwitterと連携

‣ ログイン処理、ツイート、ユーザー情報取得等16

Page 17: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

企業における応用方法

17

Page 18: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

人間系 • 広報

• リクルーティングツール‣ 社内の雰囲気を伝える‣ 会社説明会開催日程などを告知

18

Page 19: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

• フラッシュマーケティング‣ 「このツイートを見た方先着n名様に・・」

• 新製品入荷のお知らせ• 宣伝キャンペーン

• 簡易サポート窓口オリンパスイメージング http://fotopus.com/tough/news/c110304a/ より

人間系

19

Page 20: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

• Webサイトの更新情報

• プレスリリースの発信‣ ターゲットリーチまでのリードタイムを圧縮• メンテナンス・障害情報を告知‣ Webサイトダウン中はWebで告知できない!

人間系

20

Page 21: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

• 非常時の連絡手段

ASCII.jp 首都圏企業の95%「ソーシャルメディアが安否確認に有効」よりhttp://ascii.jp/elem/000/000/605/605027/

人間系

21

Page 22: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

システムから人間系• 社内イントラサイトの更新情報発信• サービス監視‣ バッチ起動・完了通知‣ 緊急性の低い警告の通知

22

Page 23: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

人間系からシステム• 任意のタイミングでリモートバッチ起動

• 派遣/出向先における出退勤管理

23

Page 24: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

システム間インテグレーション

• Twitterをハブにシステムを統合

24

Page 25: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

実装方法

人間系:

• Twitterアプリ• CoTweet• HootSuite

など既存のアプリケーション、サービスて対応可能

システム系:

アプリケーションの作り込みが必要

25

Page 26: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

既存のアプローチと比較して• 利点‣ セキュア‣ api.twitter.com:443への

outboundアクセスのみ

‣ inboutアクセス許可不要

‣ シンプルな管理‣ メッセージの配信先のメンテナンスが容易

‣ 作り込み、設定を大幅に削減

• 難点‣ Twitter APIは時折不安定

‣ サービスにSLAがない

26

API不安定時に対応する作り込みまたは運用が必要

(C) Yiying Lu

Page 27: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

JavaEEとTwitter

27

Page 28: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

JavaEEとTwitter

• Twitter4J

‣ オープンソース(Apache License 2.0)

‣ 日本語コミュニティ‣ Twitter APIを全てサポート

‣ Twitter API用ライブラリのデファクトスタンダード

Twitter4Jhttp://twitter4j.org/

28

Page 29: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter4Jの基本• http://twitter4j.org/ よりダウンロード

29

Page 30: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter4Jの基本(1)• dev.twitter.comよりアプリケーションを登録

30

Page 31: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter4Jの基本(2)• コンシューマキー、アクセストークンを取得

• twitter4j.propertiesに設定oauth.consumerKey=[コンシューマキー]oauth.consumerSecret=[コンシューマシークレット]oauth.accessToken=[アクセストークン]oauth.accessTokenSecret=[アクセストークンシークレット]

参考: Twitter4J - 設定 http://twitter4j.org/ja/configuration.html

twitter4j.properties

31

Page 32: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter4Jの基本(3)• Twitterインスタンスの取得とAPI呼び出し

参考: Twitter4J - コード例 http://twitter4j.org/ja/code-examples.html

import twitter4j.*;import javax.servet.*;import javax.servet.http.*;

public class HelloTwitterServlet extends HTTPServlet{ protected void doGet(ServletRequest req, ServletResponse res){ Twitter twitter = new TwitterFactory().getInstance(); twitter.updateStatus(“Hello World!”); req.getRequestDispatcher().forward(“WEB-INF/jsps/index.jsp”); }}

32

Page 33: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

• アプリケーションのパッケージングTwitter4Jの基本(4)

33

Page 34: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter4Jの基本(5)• サポートしているメソッドの確認

参考: Twitter4J - サポートAPI http://twitter4j.org/ja/api-support.html

34

Page 35: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

アプリケーション開発のコツ

35

Page 36: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

アプリケーション開発のコツ• セキュリティ• 堅牢なアプリケーションにするために• トランザクションとTwitter API

36

Page 37: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

セキュリティ• ツイートの可視性‣ デフォルトでTwitterはすべて公開

‣ センシティブなアカウントはプロテクトする

‣ ホワイトリスト入りのアカウントのみ閲覧可能• ダイレクトメッセージ利用の検討‣ DMは全て非公開

37

Page 38: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

セキュリティ• API通信の傍受対策

‣ SSLで通信

‣ twitter4j.properties で useSSL パラメータを設定しておく

(Twitter4JはデフォルトHTTP)

http.useSSL=truetwitter4j.properties

38

Page 39: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

堅牢なアプリケーションにするために

• Twitter APIはしばしば不安定

‣ 確実にAPI呼び出しを終える仕組みが必要

• カジュアルなリトライ‣ Twitter4Jの機能を利用

• 厳密なリトライ‣ MDBで実装

‣ リトライはアプリケーションサーバに任せる39

Page 40: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

カジュアルなリトライ• Twitter4Jの機能を利用

• 指定回数・指定間隔でリトライ

http.retryCount=5http.retryIntervalSecs=10

twitter4j.properties

40

Page 41: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

厳密なリトライ(1/2)• Message Driven Beanで実装

package twittermdb;

import javax.jms.MessageListener;

@MessageDriven(mappedName="jms/Queue")public class TwitterMDB implements MessageListener {

Twitter twitter = new TwitterFactory().getInstance();

public void onMessage(javax.jms.Message msg) { try{ twitter.updateStatus(((TextMessage)msg).getText()); }catch(TwitterException te){ throw new EJBException(te); } }

}

41

Page 42: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

厳密なリトライ(2/2)• キューの再配信とエラー宛先を指定

例外発生時に5000ミリ秒待機

最大再配信回数

再配信回数オーバー時に転送されるキュー

42

Page 43: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

トランザクションとTwitter API

• Twitter APIは非トランザクショナル‣ afterCompletionでトランザクションと同期

import javax.ejb.*;

@Stateless(name = "tweet", mappedName = "tweet")public class TweetSLSB { public void sayHello(){ System.out.println("hello"); } @AfterCompletion public void afterCompletion(boolean committed){ if(committed){ try{ twitter.updateStatus(((TextMessage)msg).getText()); }catch(TwitterException te){ throw new EJBException(te); } } }}

43

Page 44: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

まとめ• エンタープライズアプリケーションでも

Twitterは様々な応用の可能性がある

• Java(EE)とTwitter APIの高い親和性

• Twitter APIの不安定な部分はJavaEE / WebLogicの機能を活用してカバー

44

Page 45: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter API ポケットリファレンス

• 好評発売中!

http://amzn.to/iUeXjm45

Page 46: エンタープライズアプリケーション とTwitterの連携

Copyright(c) Yusuke Yamamoto All rights reserved.

Twitter API ポケットリファレンス

• 好評発売中!

http://bit.ly/wls_twtr から、または #present_twtr_ref を付けてツイート!

46