python 上的简单 XMPP 机器人
我有 python 的 XMPP 机器人的简单代码和 http://xmpppy.sourceforge.net/
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xmpp
import urllib2
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('ipbot.conf')
##########################
user= (config.get('account', 'login'))
password=(config.get('account', 'password'))
presence=(config.get('presence','presence'))
##########################
jid=xmpp.protocol.JID(user)
client=xmpp.Client(jid.getDomain())
client.connect()
client.auth(jid.getNode(),password)
################Parse IP##################
strURL='http://api.wipmania.com/'
f = urllib2.urlopen(urllib2.Request(strURL))
response = f.read()
ipget= response.split("<br>")
f.close()
#############################################
def status(xstatus):
status=xmpp.Presence(status=xstatus,show=presence,priority='1')
client.send(msging)
def message(conn,mess):
global client
if ( mess.getBody() == "ip" ):
client.send(xmpp.protocol.Message(mess.getFrom(),ipget[1]+" => "+ipget[0]))#Send IP
client.RegisterHandler('message',message)
client.sendInitPresence()
while True:
client.Process(1)
请告诉我,如何翻译此代码以使用 http://wokkel.ik.nu/ 和twistedmatrix.com/ 多谢。
I have the simple code of XMPP bot by python and http://xmpppy.sourceforge.net/
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xmpp
import urllib2
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('ipbot.conf')
##########################
user= (config.get('account', 'login'))
password=(config.get('account', 'password'))
presence=(config.get('presence','presence'))
##########################
jid=xmpp.protocol.JID(user)
client=xmpp.Client(jid.getDomain())
client.connect()
client.auth(jid.getNode(),password)
################Parse IP##################
strURL='http://api.wipmania.com/'
f = urllib2.urlopen(urllib2.Request(strURL))
response = f.read()
ipget= response.split("<br>")
f.close()
#############################################
def status(xstatus):
status=xmpp.Presence(status=xstatus,show=presence,priority='1')
client.send(msging)
def message(conn,mess):
global client
if ( mess.getBody() == "ip" ):
client.send(xmpp.protocol.Message(mess.getFrom(),ipget[1]+" => "+ipget[0]))#Send IP
client.RegisterHandler('message',message)
client.sendInitPresence()
while True:
client.Process(1)
Please, tell me, how to translate this code to use http://wokkel.ik.nu/ and twistedmatrix.com/
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的代码应该可以做到这一点。一些注意事项:
子协议,通常按概念特征、命名空间或每个
XEP。
XMPPClient
是一个建立连接的所谓流管理器,并负责与服务器的身份验证。它与
连接子协议处理程序来处理带有 XML 流的流量
管理。如果连接丢失,它会自动重新连接。
对正文中带有
ip
的每条传入消息执行此操作。status
。我现在使用PresenceProtocol
子协议处理程序,用于每次发送出席信息连接已建立并且身份验证已进行。
该示例是一个所谓的 Twisted 应用程序,将使用 twistd 启动,如文档字符串中所述。这将使进程守护进程并将日志发送到
twisted.log
。如果您指定-n
(在-y
之前),它将不会分离并记录到控制台。The following code should do it. A few notes:
subprotocols, usually split up by conceptual feature, by namespace or per
XEP.
XMPPClient
is a so-called stream manager that establishes connections,and takes care of authentication with the server. It works with the
hooked-up subprotocol handlers to process traffic with the XML stream it
manages. It automatically reconnects if the connection has been lost.
done for each incoming message with
ip
in the body.status
was never called. I now the usePresenceProtocol
subprotocol handler to send out the presence each timea connection has been established and authentication has taken place.
The example is a so-called Twisted Application, to be started using
twistd
, as mentioned in the docstring. This will daemonize the process and logs go totwisted.log
. If you specify-n
(before-y
), it will not detach and log to the console instead.