2013-06-03 40 views
0

這是一個非常具體的問題,但我找不到任何有關如何操作的文檔。 Facebook的文檔是pretty vague,一些可怕的和無用的PHP示例(實際上,它的代碼就像Facebook PHP示例代碼一樣,讓人覺得PHP很糟糕),但是我找不到任何有關Python的東西。在Python中使用訪問令牌發送XMPP的Facebook消息

我甚至無法解決如何將相同的原理從PHP示例代碼應用到Python世界。 xmpppy和SleekXMPP文檔有點空白(或broken),Google僅顯示使用密碼的人員的示例。

我有來自數據庫的訪問令牌,我沒興趣產生瀏覽器來查找內容,或者做其他任何事情來查找令牌。我有他們,認爲它是一個硬編碼的字符串。我想將該字符串傳遞給XMPP併發送消息,這就是整個事物的範圍。

有什麼建議嗎?

回答

2

我回答了一個鏈接到我寫的博客的鏈接,因爲它完美地描述瞭解決方案,但顯然這讓一些版主惱火。

雖然這顯然是荒謬的,這裏是轉發的答案。

import sleekxmpp 
import logging 

logging.basicConfig(level=logging.DEBUG) 

class SendMsgBot(sleekxmpp.ClientXMPP): 
    def init(self, jid, recipient, message): 
     sleekxmpp.ClientXMPP.__init__(self, jid, 'ignore') 
     # The message we wish to send, and the JID that 
     # will receive it. 
     self.recipient = recipient 
     self.msg = message 
     # The session_start event will be triggered when 
     # the bot establishes its connection with the server 
     # and the XML streams are ready for use. We want to 
     # listen for this event so that we we can initialize 
     # our roster. 
     self.add_event_handler("session_start", self.start, threaded=True) 
    def start(self, event): 
     self.send_presence() 
     self.get_roster() 
     self.send_message(mto=self.recipient, mbody=self.msg, mtype='chat') 
     # Using wait=True ensures that the send queue will be 
     #emptied before ending the session. 
     self.disconnect(wait=True) 

我猛的,在一個名爲fbxmpp.py,然後在另一個文件(你的員工,你的命令行應用程序,您的水壺控制器,等等),你需要像下面這樣:

from fbxmpp import SendMsgBot 

# The "From" Facebook ID 
jid = '[email protected]' 

# The "Recipient" Facebook ID, with a hyphen for some reason 
to = '[email protected]' 

# Whatever you're sending 
msg = 'Hey Other Phil, how is it going?' 

xmpp = SendMsgBot(jid, to, unicode(msg)) 

xmpp.credentials['api_key'] = '123456' 
xmpp.credentials['access_token'] = 'your-access-token' 

if xmpp.connect(('chat.facebook.com', 5222)): 
    xmpp.process(block=True) 
    print("Done") 
else: 
    print("Unable to connect.") 
1

下面的代碼工作,但只有一些修改後this thread

+0

我已經實現了反饋中提到,謝謝。 –