【问题标题】:Send facebook messages via SleekXMPP通过 SleekXMPP 发送 facebook 消息
【发布时间】:2013-09-19 23:15:46
【问题描述】:

我正在尝试在 Facebook 聊天中使用 slimXMPP 发送消息,使用此处的答案作为样板:Send a Facebook Message with XMPP using Access Tokens in Python

我的代码是

import sleekxmpp

class SendMsgBot(sleekxmpp.ClientXMPP):
    def init(self, jid, recipient, message):

        print "..."

        sleekxmpp.ClientXMPP.__init__(self, jid, 'ignore')
        self.recipient = recipient
        self.msg = message
        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')

        self.disconnect(wait=True)



if __name__ == "__main__":
    xmpp = SendMsgBot(from_id, to_id, unicode(message))

    xmpp.credentials['apikey'] = api_key
    xmpp.credentials['accesstoken'] = o_auth_token

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

但是,当我运行脚本时,我收到以下错误消息:

Traceback (most recent call last):
  File "sendMessagesScript.py", line 33, in <module>
    xmpp = SendMsgBot(from_id, to_id, unicode(message))
  File "/Library/Python/2.7/site-packages/sleekxmpp/clientxmpp.py", line 112, in __init__
    self.register_plugin('feature_starttls')
  File "/Library/Python/2.7/site-packages/sleekxmpp/basexmpp.py", line 264, in register_plugin
    pconfig = self.plugin_config.get(plugin, {})
AttributeError: 'unicode' object has no attribute 'get'

任何想法都将不胜感激!

【问题讨论】:

    标签: python xmpp facebook-chat


    【解决方案1】:

    在类SendMsgBot(sleekxmpp.ClientXMPP):中,你需要改变

    def init(self, jid, recipient, message)def __init__(self, jid, recipient, message)

    我希望它会起作用。

    【讨论】:

    • 欢迎来到 StackOverflow。 - 有点解释或参考会很好。大多数人更感兴趣的是真正了解他们在做什么。
    【解决方案2】:

    此外,似乎在原始代码中省略了一些重要的破折号。

    我也不得不改变

    xmpp.credentials['apikey'] = api_key
    xmpp.credentials['accesstoken'] = o_auth_token
    

    xmpp.credentials['api_key'] = api_key
    xmpp.credentials['access_token'] = o_auth_token
    

    这些显然是 Facebook 期望的参数名称,正如您在 Facebook 的 PHP example 中看到的那样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 2017-04-08
      • 1970-01-01
      相关资源
      最近更新 更多