Anasayfa » Ruby » Facebook Chat Api

Facebook Chat Api

13 Nisan 2010  |  Yazar: coders  |  Yorum Yok  |  15 kez okundu
Facebook'da Paylaş Twitter'da Paylas FriendFeed'de Paylaş Facebook Chat Api

servisine bağlanacağız.Oturum açarak facebook
üzerinden mevcut hesabımız ile online olan kişilerle chat imkanı
sağlayacağız..

Şimdi api mize geçelim..

require 'mechanize'

require 'json'

require 'ostruct'

require 'pp'

class FacebookSohbet

def intialize (email, pass); @email, @pass = email, pass; end

def login

@agent = WWW ::Mechanize.new

@agent.user_agent_alias = 'Windows IE 7'

f = @agent.get("http://facebook.com/login.php").forms.first

f.fields.name("email").value =@email

f.fields.name("pass").value = @pass

f.submit

body = @agent.get ("http://www.facebook.com/home.php").body

#Facebook anasayfasında ayrıştırma işlemini gerçekleştiriyoruz..

@uid=%r{<a href=".+?/profile.php?id=(d+)" class="profile_nav_link">Profile</a>}.match(body [1].to_i

@channel = %r{"channel (d+)"} .match(body [1]

@post_form_id = %r{<input type="hidden" id="post_form_id" name="post_form_id" value="([^"]+)}.match(body)[1]

end

def wait for_messages

determine_initial_seq_number unless @seq

begin

json = parse_json @agent.get(get_message_url (@seq)).body

end while j.son["t"] == "continue"  Herhangi bir mesajınız yok..Bekleyiniz..

@seq += 1

json["ms"].select{|m | m ['type'] == 'msg' } .map do | msg |

info = msg.delete 'msg'

msg ['text'] = info ['text']

msg ['time'] = Time.at(info['time']/1000)

OpenStruct.new msg

end.reject { |msg| msg.from == @uid } #Mesaj geçmişini temizliyoruz bu kısımda

end

def send_message(uid, text)

r= @agent.post "http://www.facebook.com/ajax/chat/send.php",

:msg_text => text,

:msg_id => rand (999999999),

:client_time => (Time.now.to_f*1000). to_i,

:post_form_id => @post_form_id

end

def buddy_list

json = parse_json(@agent.post("www.facebook.com/ajax/presence/update.php",

:buddy_list =>1 , : post_form_id => @post_form_id, :user => @uid).body)

json['payload'] ['buddy_list'] ['userInfos'].inject ({}) do |hast, (uid, info) |

hash.merge uid => info ['name']

end

end

private

def determine_initial_seq_number

json = parse_json @agent.get (get_message_url (-1)).body

@seq = json ["seq"].to_i

end

def get_message_url (seq)

"http://0.channel#{@channel}.facebook.com/x/0/false/p_#{@uid}=#{seq}"

end

#etkileşimsiz json elementlerini sonlandırabiliriz..

def parse_json(s)

JSON.parse s.sub (/^[^{]+/, '')

end

end

if __FILE__ == $0

fb = FacebookSohbet.new (ARGV.shift, ARGV.shift)

fb.login

puts "Buddy Lİst:"

pp fb.buddy_list

Thread.abort_on_exception = true

Thread.new do

puts usage = "Arkadaş listesine bakmak için <facebook_id> parametresini kullanıyoruz.Yani mesaj gönderirken id numaranızı

(örneğin: 11111 selam thecoders ailesi) şeklinde gönderiyoruz..

loop do

case gets.strip

when 'buddy' then pp fb.buddy_list

when /^(d+) (.+)$/

uid, text = $1. to_i, $2

fb.send_message(uid, text)

else

puts usage

end

end

end

#Karşı bağlantıdan mesaj almak için ise kullanacağımı döngü şu şekilde..

loop do

fb.wait_for_message.each do |msg|

puts"[#{msg.time.strftime('%H:%M')}] #{msg.from_name} (#{msg.from}) : #{msg.text}"

end

end

end
Facebok'ta Paylaş

Benzer Yazılar

Etiketler: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
avatar

Ahmet Ates

http://www.coders.gen.tr/ 25 yasindayim. Z.K.U Biyomedikal Cihaz Teknolojisi bölümünü okudum, Programlama dilleri arasindan ilgilendiğim ve profesyonel olarak hizmet verdiğim dil Fortran'dir. Web olarak Php,Css ve hazır sistemler olarak Wordpress ve Vbulletine hayranlık besliyorum.

Bu yazı hakkında birşeyler demek ister misiniz?

RSS üzerinden bu yazıya yapılan yorumları takip edin.

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir.

Şu HTML etiketlerini ve özelliklerini kullanabilirsiniz:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Programlama