1

我目前有一个 skypebot,当我使用以下代码时,它会回复命令和 ping 网站:

 if Status == 'SENT' or (Status == 'RECEIVED'):
    if Message.Body.lower() == '!ping google':
        ping = os.system("ping google.com")
        if ping == 0:
            Message.Chat.SendMessage("Online!")
        else:
            Message.Chat.SendMessage('Offline!')

这有效,如果网站在线,它将显示在线!在聊天中。但是,它需要我事先定义网站。我已经搜索了好几个小时,试图找到我将如何做到这一点,以便我可以执行 !ping [website] 并允许用户随时使用他们想要的任何网站。有任何想法吗?

4

2 回答 2

0

我也制作了一个 SkypeBot。我使用http://www.downforeveryoneorjustme.com/
我这样做:
Functions.py

    def isUP(url):
try:
    source = urllib2.urlopen('http://www.downforeveryoneorjustme.com/' + url).read()
    if source.find('It\'s just you.') != -1:
        return 'Website Responsive'
    elif source.find('It\'s not just you!') != -1:
        return 'Tango Down.'
    elif source.find('Huh?') != -1:
        return 'Invalid Website. Try again'
    else:
        return 'UNKNOWN'
except:
    return 'UNKNOWN ERROR'    


对于commands.py

                        elif msg.startswith('!isup '):
                    debug.action('!isup command executed.')
                    send(self.nick + 'Checking website. Please wait...')
                    url = msg.replace('!isup ', '', 1)
                    url = functions.getCleanURL(url)
                    send(self.nick + functions.isUP(url))

当然在 commands.py 文件中使用“导入函数” 。
我相信你可以稍微改变一下来检查你的机器人网站的状态。
祝你好运 :)

于 2013-12-15T11:20:06.053 回答
0

我会做这样的事情:

body = Message.Body

if body.startswith('!'):
    parts = body.split()    # ['!ping', 'google.com']
    command = parts[0][1:]  # 'ping'

    result = commands[command](*parts[1:]) # Calls `ping` with 'google.com'
    Message.Chat.SendMessage(result)  # Prints out the resulting string

现在,您可以定义简单的函数:

def ping(url):
    if os.system("ping " + url) == 0:
        return 'Online!'
    else:
        return 'Offline!'

并将它们添加到命令字典中:

commands = {
    'ping': ping
}

os.system()如果您期望任意用户输入是不安全的,所以我会subprocess.Popen改用(或者只是尝试使用 Python 连接到网站)。

于 2013-06-08T18:46:08.617 回答