たいやきブログ

mail to tsuyoshi.ogawa[a]gmail.com if you have some question!

Facebook Chat APIをrubyでつかってみたよ

https://developers.facebook.com/docs/chat/

Facebook連携アプリを作る際に、アプリを認証してくれたAさんからAさんの友達のBさんにメッセージを送るには2つの方法があります。1つは、Mention Taggingでもう一つは、chat。前者はAさんのWallとBさんのWallに表示されるので(Bさんが許可してれば)Publicですが、後者はPrivateなものになります。今回は、Privateなメッセージをおくるためのchat APIについて解説してみます。

ドキュメントにある通り、FacebookのChatはXMPPというプロトロルを使用しているらしく、これは今はなきGoogle Waveも採用していたプロトコルですね。あー、懐かしい。。
で、細かいことは抜きに動くものが欲しい人は↓のブログを参照するといいと思います。
http://dalibornasevic.com/posts/35-how-to-send-private-messages-with-facebook-api
permissionとしてxmpp_logicが必要なのが注意です。コード自体は数行なのですぐにお試しいただけるかと思います。

あと、注意点として、ちゃんと規約を守りましょうということで、以下のBest Practicesに反する行為をしていないかをよくよく確認してからアプリをつくりましょう。Chatの中身はユーザがタイプしたものじゃないとダメとか、URL、広告は入れちゃダメとかはGraph API、Open Graphの規約と似ていますね。

Best Practices

In order to provide the best user experience, we recommend your chat integration do the following:

Your Facebook Chat integration should only be used for applications facilitating real time conversation or interaction between users. Links or advertisements should not be sent via chat, unless the sending user types in this message.
Your Facebook Chat integration should only be used for sessions that are expected to be long-lived. Clients should not rapidly churn on and off.
vCards retrieved through Facebook Chat will contain profile pictures if available. Clients should cache these pictures using the content hash, not the user ID, as the key. vCards should only be fetched if the client does not already have that user's picture cached.
Clients should not automatically reconnect if they receive a stream-error of type conflict.
Clients should be able to handle a single contact with multiple group elements.
Incoming messages from the JIDs chat.facebook.com or facebook.com should be displayed as administrative messages.