通过SleekXMPP传送Facebook讯息(Send facebook messages via

2019-10-18 15:33发布

我试图与Facebook的sleekXMPP聊天消息发送,使用的答案从这里作为一个样板: 发送一个Facebook邮件使用访问令牌在Python XMPP

我的代码

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'

任何想法,将不胜感激!

Answer 1:

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

DEF的init(自,JID,收件人,消息)发送__变形点焊初始化 __(个体,JID,收件人,消息)

我希望它会工作。



Answer 2:

此外,似乎有些重要的破折号已经从原来的代码ommitted。

我也不得不改变

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看例子



文章来源: Send facebook messages via SleekXMPP