2

我希望能够使用 Python 检索用户 Google Talk 状态消息,很难找到有关如何使用其中一些库的文档。

4

1 回答 1

0

我没有安装 xmpp 的任何东西,但这里有一些旧代码我躺在那里可能会对你有所帮助。出于测试目的,您需要将 USERNAME/PASSWORD 更新为您自己的值。

注意事项:登录到 Google Talk 的用户会在其用户 ID 上获得一个随机存在字符串:如果您尝试获取其他用户的状态,这无关紧要,但如果您想编写一些代码,那么想与之交流您自己需要区分从 GMail 登录的用户或从测试程序登录的 GTalk 客户端。因此,代码通过用户 ID 进行搜索。

此外,如果您在登录后立即阅读状态,您可能什么也得不到。代码中存在延迟,因为状态变为可用需要一点时间。

"""Send a single GTalk message to myself"""

import xmpp
import time

_SERVER = 'talk.google.com', 5223
USERNAME = 'someuser@gmail.com'
PASSWORD = 'whatever'


def sendMessage(tojid, text, username=USERNAME, password=PASSWORD):
    jid = xmpp.protocol.JID(username)
    client = xmpp.Client(jid.getDomain(), debug=[])
    #self.client.RegisterHandler('message', self.message_cb)
    if not client:
      print 'Connection failed!'
      return
    con = client.connect(server=_SERVER)
    print 'connected with', con
    auth = client.auth(jid.getNode(), password, 'botty')
    if not auth:
      print 'Authentication failed!'
      return
    client.RegisterHandler('message', message_cb)
    roster = client.getRoster()
    client.sendInitPresence()
    if '/' in tojid:
        tail = tojid.split('/')[-1]
        t = time.time() + 1
        while time.time() < t:
            client.Process(1)
            time.sleep(0.1)
            if [ res for res in roster.getResources(tojid) if res.startswith(tail) ]:
                break
        for res in roster.getResources(tojid):
            if res.startswith(tail):
                tojid = tojid.split('/', 1)[0] + '/' + res

    print "sending to", tojid
    id = client.send(xmpp.protocol.Message(tojid, text))
    t = time.time() + 1
    while time.time() < t:
        client.Process(1)
        time.sleep(0.1)
    print "status", roster.getStatus(tojid)
    print "show", roster.getShow(tojid)
    print "resources", roster.getResources(tojid)
    client.disconnect()

def message_cb(session, message):
    print ">", message

sendMessage(USERNAME + '/Talk', "This is an automatically generated gtalk message: did you get it?")
于 2010-09-30T18:04:55.190 回答