使用sleekXMPP或python中的另一个客户端接收GChat消息?

2024-09-23 22:26:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有用于python的sleekXMPP,我使用API创建了发送消息的函数,但是当我研究接收消息时,我找不到任何东西,有人能帮我解决这个问题吗,或者反驳这种可能性。谢谢。 下面是我用来发送消息的代码,如果有帮助的话。在

to = config.get('usermap', to[4:])
gmail_from_user = config.get('auth', 'email')
gmail_from_secret = config.get('auth', 'secret')

sys.stdout = stdouttmp
sys.stderr = stderrtmp
print "Sending chat message to " + to
xmpp = SendMsgBot(gmail_from_user, gmail_from_secret, to, message)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0199') # XMPP Ping

sys.stdout = stdouttmp
if xmpp.connect(('talk.google.com', 5222)):
    xmpp.process(block=True)
else:
    sys.stdout = stdouttmp
    print("Unable to connect.")

sys.stdout = stdouttmp
sys.stderr = stderrtmp

顺便说一句,我使用一个.cfg文本文件作为用户的电子邮件和密码,以及一些联系人,然后在中进行解析


Tags: tofromauthconfig消息getsecretstderr
1条回答
网友
1楼 · 发布于 2024-09-23 22:26:10

我看到您使用的是send_client.py示例。该示例的目的是如何可靠地登录、发送一条消息,然后注销。您的用例是同时发送和接收消息,所以您最好看看echo_client.py示例。在

值得注意的是,为了接收消息,您应该:

# in your __init__ method:
def __init__(...):
    # ...
    self.add_event_handler('message', self.recv_message)

def recv_message(self, msg):
    # You'll probably want to ignore error and headline messages.
    # If you want to handle group chat messages, add 'groupchat' to the list.
    if msg['type'] in ('chat', 'normal'):
        print "%s says: %s" % (msg['from'], msg['body'])

同样,您需要从使用SendMsgBot示例切换,因为它在发送消息后会自动断开连接。在

别忘了sleek@conference.jabber.org聊天室,如果你需要帮助。在

兰斯

相关问题 更多 >