使用Python中的访问令牌发送带有XMPP的Facebook消息 [英] Send a Facebook Message with XMPP using Access Tokens in Python

查看:150
本文介绍了使用Python中的访问令牌发送带有XMPP的Facebook消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常具体的问题,但我找不到任何有关如何做到的文档。 Facebook文档是很模糊的,有些可怕的和无用的PHP的例子(真的,它的代码像Facebook的PHP示例代码,让人们认为PHP愚蠢),但我找不到任何关于Python。

This is a very specific question, but I cannot find any documentation on how I can do it. The Facebook Documentation is pretty vague with some horrible and useless PHP examples (really, it's code like the Facebook PHP Sample Code that make people think PHP sucks) but I cannot find anything around for Python.

我甚至不能工作了解如何将相同的原理从PHP示例代码应用到Python世界。 xmpppy和SleekXMPP文档有点裸露(或 broken ),Google仅显示使用密码的人的示例。

I can't even work out how to apply the same principles from the PHP sample code into a Python world. The xmpppy and SleekXMPP docs are a bit bare (or broken) and Google only shows examples of people using passwords.

我有来自数据库的访问令牌,我没兴趣产生浏览器来查找内容,或做任何其他事情来找到令牌。我有他们,认为它是一个硬编码的字符串。我想将该字符串传递给XMPP并发送消息,这是整个事情的范围。

I have the access tokens coming from the database, I have no interest in spawning a browser to find stuff, or doing anything else to find a token. I have them, consider it a hardcoded string. I want to pass that string to XMPP and send a message, that is the whole scope of things.

任何建议?

推荐答案

我回答这个链接到我写的一个博客,因为它描述了完美的解决方案,但显然是恼火的一些主持人。

I answered this with a link to a blog I wrote because it described the solution perfectly, but apparently that annoyed some moderators.

虽然这显然是荒谬的,这里是回覆的回覆。

While that's clearly ridiculous, here is the answer reposted.

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,然后在另一个文件(您的工作人员,您的命令行应用程序,您的Flask控制器,无论如何),您将需要以下内容:

I shoved that in a file called fbxmpp.py, then in another file (your worker, your command line app, your Flask controller, whatever) you'll need something like the following:

from fbxmpp import SendMsgBot

# The "From" Facebook ID
jid = '511501255@chat.facebook.com'

# The "Recipient" Facebook ID, with a hyphen for some reason
to = '-1000023894758@chat.facebook.com'

# 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.")

这篇关于使用Python中的访问令牌发送带有XMPP的Facebook消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆