如何根据浏览器内容创建基本的聊天机器人/自动应答器? (Applescript,或其他建议?)

发布于 2024-11-25 09:21:20 字数 594 浏览 0 评论 0原文

我是一名(非常)初学者程序员,我想知道编写一个非常简单的聊天室机器人是否可行,该机器人很大程度上取决于所使用的聊天室类型。

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

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

本质上,流程类似于:

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

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

I am a (very) beginner programmer and I am wondering if it would be feasible to write a very simple chat room bot which is fairly dependent of what kind of chat room is being used.

The scenario is that a friend of mine has a basic chat room set up (https://blueimp.net/ajax/) just for a few buddies to use, but I want to make a "bot" account that exists only on the client machine (mine). So, it would continually check the browser (without reloading the page) for a particular string and then respond if it detects it. Just as an example, maybe they would type !bot song and it would return with a song recommendation.

I was thinking Applescript could be an easy way to do this. Can anyone possibly help get me started? Like I said, I'm a beginner so please keep that in mind. Trying to use this as a learning experience, and I learn best by trying to come up with a solution to a particular scenario rather than by books or tutorials.

Essentially, the flow would be something like:

  • Check webpage for string every 2 seconds (it's Ajax-based, no need to refresh... just check the browser window itself)
  • If string is found, reply in the first text field with response + enter

I know this isn't the most efficient way to make a chat bot, I'm just trying to use this scenario to help me understand how applications interact with each other locally. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2024-12-02 09:21:20

我希望我没有通过向您提供此代码来破坏 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

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

I hope I'm not breaking some unwritten rule of StackOverflow by giving you this code, but you could try out this AppleScript code and see how it works (assuming the whole browser window is just the chat box, change this as needed):

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

If this doesn't work for some reason and/or you have any questions, just ask. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文