【问题标题】:Skype4py !command with argumentsSkype4py ! 带参数的命令
【发布时间】:2013-06-04 20:29:55
【问题描述】:

我目前有一个 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] 并允许用户随时使用他们想要的任何网站。有任何想法吗?

【问题讨论】:

  • 将网站作为命令行参数发送到您的脚本。

标签: python skype skype4py


【解决方案1】:

我会这样做:

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 连接到网站)。

【讨论】:

  • 干杯!作为一个新机器人完美地工作,没有与我的旧代码一起工作(它很乱),将努力重新编码它。我将如何添加其他命令,例如 !about 只是在聊天中发送消息?
  • 或自动回复器,例如“嘿 NAME”回复“我目前不在”
【解决方案2】:

我也制作了一个 SkypeBot。 我用http://www.downforeveryoneorjustme.com/
我这样做:
函数.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 文件中使用 "import functions"
我相信你可以稍微改变一下来检查你的机器人网站的状态。
祝你好运:)

【讨论】:

    猜你喜欢
    • 2012-02-25
    • 2021-01-26
    • 2017-11-22
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2013-10-07
    • 2019-10-09
    相关资源
    最近更新 更多