python - 通过 SleekXMPP 发送 facebook 消息

标签 python xmpp facebook-chat

我正在尝试使用 sleekXMPP 在 facebook 聊天中发送消息,使用此处的答案作为样板: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'

如有任何想法,我们将不胜感激!

最佳答案

在类 SendMsgBot(sleekxmpp.ClientXMPP): 中,您需要更改

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

我希望它能奏效。

关于python - 通过 SleekXMPP 发送 facebook 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18906462/

相关文章:

python - 设置 matplotlib 颜色栏范围

XMPP BOSH 与 COMET

xmpp - Dart 有 XMPP api 吗?

java - 在 facebook 聊天中获取用户 id 而不是用户名

ios - 为什么不能显示灰色气泡图像?

python - Ejabberd, Python : Check user, 如果没有创建

python - 使用 Python range 内置函数从整数向后计数,与解析树相关

python - numpy 数组上的 lambda 函数。这段代码有什么问题吗?

ios - 在 iOS 中解析 XML

xmpp - 有关 XMPP 的 C2S 和 S2S 连接类型有哪些?