Pergunta

I currently have a skypebot which replies to commands and pings websites when I use the following code:

 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!')

This works and if the website is online it will display Online! in chat. However, it requires me to define the website before hand. I have searched for a good few hours now to try to find how I would make it so I can do !ping [website] and allow for the user at any time to use whatever website they want. Any ideas?

Foi útil?

Solução

I would do something like this:

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

Now, you can define simple functions:

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

And add them to a commands dictionary:

commands = {
    'ping': ping
}

os.system() is insecure if you're expecting arbitrary user input, so I'd use subprocess.Popen instead (or just try connecting to the website with just Python).

Outras dicas

I have a SkypeBot I made as well. I use http://www.downforeveryoneorjustme.com/
I do it this way:
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'    


And for 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))

Of course with "import functions" in the commands.py file.
I'm sure you can alter this a bit to work to check a website's status for your bot as well.
Good luck :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top