1

我是一个(非常)初学者程序员,我想知道编写一个非常简单的聊天室机器人是否可行,这完全取决于正在使用的聊天室类型。

场景是我的一个朋友设置了一个基本的聊天室 (https://blueimp.net/ajax/) 仅供几个好友使用,但我想创建一个仅存在于客户端机器(我的)。因此,它会不断检查浏览器(不重新加载页面)是否有特定字符串,然后在检测到它时做出响应。举个例子,也许他们会输入!bot song并且它会返回一个歌曲推荐。

我在想 Applescript 可能是一个简单的方法来做到这一点。任何人都可以帮助我开始吗?就像我说的,我是初学者,所以请记住这一点。尝试将此作为一种学习体验,我通过尝试为特定场景提出解决方案而不是通过书籍或教程来学习最好。

本质上,流程类似于:

  • 每 2 秒检查一次网页的字符串(它基于 Ajax,无需刷新......只需检查浏览器窗口本身)
  • 如果找到字符串,请在第一个文本字段中回复 response + enter

我知道这不是制作聊天机器人的最有效方式,我只是想利用这个场景来帮助我了解应用程序如何在本地相互交互。:)

4

1 回答 1

0

我希望我给你这段代码不会违反 StackOverflow 的一些不成文规则,但你可以试试这个 AppleScript 代码,看看它是如何工作的(假设整个浏览器窗口只是聊天框,根据需要更改它):

repeat
    tell application "Safari" --or whatever browser you use
        set the message to ""
        repeat until the message contains "something" --replace 'something' with the string you want to search for
            set the message to (the text of document 1) as string --all text on the page
        end repeat
    end tell
    tell application "System Events"
        tell process "Safari"
            select text box 1 of document 1 --assuming the chat box is the only text box on the page; if not, determine which number the chat box is.
            --Text boxes are numbered starting at 1 and ending at the last text box. The 'search' goes from left to right, once the right edge of the window is reached, the search goes down until it reaches the next box.
            --Once you determine the text box's number, change the 'text box 1' to 'text box [number]', where [number] is the text box's number.
            keystroke "response" --replace 'respose' with the reply you want to send back
        end tell
    end tell
    delay 2 --wait 2 seconds so the script doesn't spam the chat box
end repeat

如果由于某种原因这不起作用和/或您有任何问题,请询问。:)

于 2011-07-22T21:04:46.857 回答