如何基于浏览器内容创建基本聊天机器人/自动响应程序?(Applescript或其他建议?)

如何基于浏览器内容创建基本聊天机器人/自动响应程序?(Applescript或其他建议?),applescript,chat,bots,chatroom,Applescript,Chat,Bots,Chatroom,我是一个(非常)初学者程序员,我想知道是否可以编写一个非常简单的聊天室机器人,它相当依赖于正在使用的聊天室类型 场景是我的一个朋友建立了一个基本的聊天室(https://blueimp.net/ajax/)只供一些朋友使用,但我想创建一个“bot”帐户,该帐户只存在于客户机(我的)上。因此,它会不断检查浏览器(无需重新加载页面)是否有特定字符串,然后在检测到该字符串时做出响应。举个例子,也许他们会键入!bot歌曲,它将返回歌曲推荐 我想Applescript可能是一种简单的方法。有人能帮我开始吗

我是一个(非常)初学者程序员,我想知道是否可以编写一个非常简单的聊天室机器人,它相当依赖于正在使用的聊天室类型

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

我想Applescript可能是一种简单的方法。有人能帮我开始吗?就像我说的,我是初学者,所以请记住这一点。尝试将此作为一种学习体验,我通过尝试提出特定场景的解决方案而不是通过书本或教程来获得最佳学习效果

从本质上讲,流程类似于:

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

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

我希望我没有因为给你这段代码而打破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