簡體   English   中英

python上的簡單XMPP機器人

[英]Simple XMPP bot on python

我有python和http://xmpppy.sourceforge.net/的XMPP bot的簡單代碼

#!/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/非常感謝。

下面的代碼應該做到這一點。 一些注意事項:

  • Wokkel使用所謂的子協議處理程序來覆蓋對特定子協議的支持,這些子協議通常按概念特征,按名稱空間或按XEP拆分。
  • XMPPClient是所謂的流管理器,它建立連接並負責與服務器的身份驗證。 它與連接的子協議處理程序一起使用,以處理其管理的XML流的流量。 如果連接丟失,它將自動重新連接。
  • 本示例定義了一個新的處理程序來處理傳入的消息。
  • 與原始代碼不同,在這里,對於正文中帶有ip每個傳入消息,都完成了檢索IP地址的請求。
  • 在原始代碼中,從未調用status 現在,我使用PresenceProtocol協議處理程序在每次建立連接並進行身份驗證時發出狀態信息。

的例子是一個所謂的扭曲的應用,使用將要開始twistd如在文檔字符串所提到的,。 這將守護進程,並且日志進入twisted.log 如果指定-n (在-y之前),它將不會分離並登錄到控制台。

#!/usr/bin/python

"""
XMPP example client that replies with its IP address upon request.

Usage:

    twistd -y ipbot.tac
"""

import ConfigParser

from twisted.application import service
from twisted.python import log
from twisted.web.client import getPage
from twisted.words.protocols.jabber.jid import JID
from twisted.words.protocols.jabber.xmlstream import toResponse

from wokkel.client import XMPPClient
from wokkel.xmppim import PresenceProtocol, MessageProtocol

class IPHandler(MessageProtocol):
    """
    Message handler that sends presence and returns its IP upon request.

    @ivar presenceHandler: Presence subprotocol handler.
    @type presenceHandler: L{PresenceProtocol}

    @ivar show: Presence show value to send upon connecting.
    @type show: C{unicode} or C{NoneType}
    """

    def __init__(self, presenceHandler, show=None):
        self.presenceHandler = presenceHandler
        self.show = show


    def connectionInitialized(self):
        """
        Connection established and authenticated.

        Use the given presence handler to send presence.
        """
        MessageProtocol.connectionInitialized(self)
        self.presenceHandler.available(show=self.show, priority=1)


    def onMessage(self, message):
        """
        A message has been received.

        If the body of the incoming message equals C{"ip"}, retrieve our
        IP address and format the response message in the callback.
        """
        def onPage(page):
            address, location = page.split(u"<br>")
            body = u"%s => %s" % (location, address)
            response = toResponse(message, stanzaType=message['type'])
            response.addElement("body", content=body)
            self.send(response)

        if unicode(message.body) != u"ip":
            return

        d = getPage("http://api.wipmania.com")
        d.addCallback(onPage)
        d.addErrback(log.err)



# Read the configuration file
config = ConfigParser.ConfigParser()
config.read('ipbot.conf')

user =  config.get('account', 'login')
password = config.get('account', 'password')
presence = config.get('presence','presence')

# Set up a Twisted application.
application = service.Application('XMPP client')

# Set up an XMPP Client.
jid = JID(user)
client = XMPPClient(jid, password)
client.logTraffic = True
client.setServiceParent(application)

# Add a presence handler.
presenceHandler = PresenceProtocol()
presenceHandler.setHandlerParent(client)

# Add our custom handler
ipHandler = IPHandler(presenceHandler, presence)
ipHandler.setHandlerParent(client)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM