开发者

How to create a basic chatbot/auto responder based on browser contents? (Applescript, or other recommendations?)

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 (mi开发者_运维百科ne). 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. :)


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. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜