Facebook Chat Api
Ruby ile facebook 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
Benzer Yazılar
- Xml – Csv Convert İşlemi
- URL Encode Etmek
- Ruby Mysql Bağlantısı
- Ruby Server
- Ruby TCP/IP
- Mail Gönderme
- Ruby – Basit Örnekler
- Ruby – Giriş
- Ruby Nedir?
- Ruby Kurulumu
Etiketler: cod, code, coder, coders, diller, Facebook API kodlari, Facebook API kodu, Facebook Bağlanma, Facebook Bağlanma eklentisi, Facebook Chat, Facebook Chat Api, Facebook kodlari, Facebook oto giris, Facebook sidebar, kodlama, program, Program Dilleri, program yazmak, programlama, programlama dilleri, Ruby, Ruby Dili, Ruby eklentileri, Ruby full download, Ruby full indir, Ruby Gorsel Anlatim, Ruby Hatalari, Ruby ile facebook, Ruby kodlama, Ruby kodlari, Ruby komutlari, Ruby kurulumu, Ruby Nedir, Ruby özellikleri, Ruby programlari, Ruby Sorunlari, system developers, turk coders, Yazılım, Yazılım Dilleri, Yazılım Geliştiricileri














