I make a chat assistant on the site (chat bot, if you like), and ran into the problem of checking the keyword (the "! Help" commands). Perform a check in the timer. Wrote a primitive variant with the search for a keyword in the source code recorded in StringList :

 find:='!help'; SrcCode.Clear; SrcCode.Add(GetHtml(WB_Doc)); for i:=0 to SrcCode.Count - 1 do if pos(find, SrcCode.Strings[i]) > 0 then begin Label1.Caption:='Ключевое слово найдено'; SpeedButton2.Click; end else Label1.Caption:='Ничего'; 

It works, but as expected, at intervals of every 3 seconds, I get a message stating that the keyword has been found and the response to the chat request is being executed.

I decided to dig in the direction of searching for a keyword in the page elements:

  DocChat: IHTMLElementCollection; chatElement: IHTMLElement; DocChat:=(webbrowser1.Document as IHTMLDocument2).all.tags('span') as IHTMLElementCollection; for i:=0 to DocChat.Length-1 do begin chatElement:=DocChat.item(i,0)as IHTMLElement; if (chatElement.className='message') and (chatElement.innerText='!help') then begin Label1.Caption:='Ключевое слово найдено'; SpeedButton2.Click; end else Label1.Caption:='Ничего'; end; 

But success in this matter was not. Not searched. If, in this case, the search was performed, the effect would still be the same (every 3 seconds “Keyword found”).

Please tell me how you can implement the search for the "! Help" command in the timer, without a looping problem. Those. How a new message appears with a keyword - run once:

  Label1.Caption:='Ключевое слово найдено'; SpeedButton2.Click; 

if the keyword is received again, execute the same commands again.

PS On the "html" page in the chat there is a div with the class chat-line , in it there is a span with the class timestamp , which records the time of the published comment (12:45) and another span , with the class message , which contains the comment itself. I think it can be used somehow, but comments with the team can be sent several times a minute, so this information is unlikely to help.

UPD: As for the sequence number, I do not quite know how the assignment code should be for the found keyword of the sequence number and the response is no more than 1 time. It should be borne in mind that through the n-th number of comments in the chat, older messages disappear from the source code.

UPD: Help please, I honestly do not know how to solve this problem.

  • one
    You need to somehow memorize the found word and not respond to it more than 1 time. For example, watch the timestamps of messages, or the sequence number or number of messages. - Kromster

1 answer 1

The option is not perfect, but you can try as an option.

  • Create a message handler, which after some time or event will update the array in which hash lines are stored (message + time + more necessary information. The more unique the line is, the better) and the counter that records the number of mentions of the keyword !help . During the next check, records that are not found are deleted, new ones are added.

  • The next scan finds all the keywords, generates a hash line for the message and checks the number from the first step, if the number is greater, then we received a new message with the keyword !help .

  • Thank you of course for taking the time and coming up with such a complex option, but to my regret I don’t know how to implement and the like. For me, so far it is very difficult. Thank. - Alexey0001 September
  • I do not know how to create a message handler, how to make an array in which the string hash will be stored. How to make a counter is still kind of understandable ( Inc ). I did not understand what is meant by "not found records are deleted, new ones are added." But under the second marker, this is the second option as I understood it, which sounds clearer, but again I don’t know how to generate hash lines and check the number of steps. - Alexey0001 September
  • If it is clear what to do and do not know how to implement it is better to create a new question. Regarding the fact that it is not clear: Not found records are deleted, it means that when the list of messages is updated and a new message is received, the old one disappeared (You yourself wrote.) So we need to control everything not to keep the "junk" records. - androschuk
  • Thank you, I created another question two days ago and solved the problem. Day - Alexey0001
  • Perfectly. Happy for you. If the current question is resolved, you can tick the answer. - androschuk