在 python xmpp 中检索 gtalk 昵称

发布于 2024-10-09 00:02:27 字数 432 浏览 1 评论 0原文

在 python xmpp 模块中,我可以检索任何联系人的昵称,如下所示:

self.connection.auth(userJid.getNode(), self.password)
self.roster = self.connection.getRoster()  
name = self.roster.getName(buddyJid)

..其中 buddyJid 的形式为 [电子邮件受保护]
现在,我需要检索对连接进行身份验证的用户的昵称 (userJid)。我用上面的方法找不到这个名字。 我可以使用哪种方法检索当前用户的名称?

In python xmpp module, I'm able to retrieve the nickname of any contacts as follows:

self.connection.auth(userJid.getNode(), self.password)
self.roster = self.connection.getRoster()  
name = self.roster.getName(buddyJid)

..where buddyJid is of the form [email protected].
Now, I need to retrieve the nickname of the user who authenticates the connection (userJid). I cannot find the name using the above method.
Which method can I use retrieve the name of the current user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉 2024-10-16 00:02:27

此信息不在名册中。您需要单独查询客户并通过发送以下 IQ 获取他们的 vCard:

<iq from='[email protected]/roundabout'
    id='v1'
    type='get'>
  <vCard xmlns='vcard-temp'/>
</iq>

This information is not in the roster. You will need to query the clients individually and get their vCard by sending this IQ :

<iq from='[email protected]/roundabout'
    id='v1'
    type='get'>
  <vCard xmlns='vcard-temp'/>
</iq>
八巷 2024-10-16 00:02:27

谢谢 nicholas_o,这是我根据您的建议编写的示例函数。 (XML 逻辑并不理想,但对于我需要的简单任务来说已经足够了)

def vcard(disp, jid):
    msg = xmpp.protocol.Iq()
    msg.setType('get')
    msg.setTo(jid)
    qc = msg.addChild('vCard')
    qc.setAttr('xmlns', 'vcard-temp')
    rep = disp.SendAndWaitForResponse(msg)
    # to see what other fields are available in the XML output:
    # print rep
    userid=fname=lname=title=department=region=None
    for i in rep.getChildren():
        for j in i.getChildren():
            if j.getName() == "TITLE":
                title = j.getData().encode('utf-8')
            for k in j.getChildren():
                if k.getName() == "GIVEN":
                    fname = k.getData().encode('utf-8')
                if k.getName() == "FAMILY":
                    lname = k.getData().encode('utf-8')
                if k.getName() == "ORGUNIT":
                    department = k.getData().encode('utf-8')
                if k.getName() == "REGION":
                    region = k.getData().encode('utf-8')
    return fname, lname, title, department, region

Thank you nicholas_o, this is a sample function I put together based your suggestion. (The XML logic isn't ideal, but it was sufficient for the simple task I needed this for)

def vcard(disp, jid):
    msg = xmpp.protocol.Iq()
    msg.setType('get')
    msg.setTo(jid)
    qc = msg.addChild('vCard')
    qc.setAttr('xmlns', 'vcard-temp')
    rep = disp.SendAndWaitForResponse(msg)
    # to see what other fields are available in the XML output:
    # print rep
    userid=fname=lname=title=department=region=None
    for i in rep.getChildren():
        for j in i.getChildren():
            if j.getName() == "TITLE":
                title = j.getData().encode('utf-8')
            for k in j.getChildren():
                if k.getName() == "GIVEN":
                    fname = k.getData().encode('utf-8')
                if k.getName() == "FAMILY":
                    lname = k.getData().encode('utf-8')
                if k.getName() == "ORGUNIT":
                    department = k.getData().encode('utf-8')
                if k.getName() == "REGION":
                    region = k.getData().encode('utf-8')
    return fname, lname, title, department, region
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文