MyChat add-ons: developing, technical questions, testing, documentation and other useful information
User avatar
Alona Kutsenko
The general idea is the following: a user opens a private dialogue with Elisa the bot (UIN = 0) and writes commands for it. If the bot understand commands, he answers to them, if not — sends a help information in return.

For example, I made a processing for IP address. You write an IP address for the bot, and he returns a country and city (GeoIP integration was enabled for this). The script uses a regular expression to identify if a line is really a text IPv4 address.

How it looks like in the scripts editor:

chat-bot.png
How MyChat bot works?
chat-bot.png (85.26 KiB) Viewed 23434 times

Script source:
Code: Select all
// ---------------------------------------
// Script created by Alexey Pikurov (UIN 1)
// 15.09.2018 14:23:27
// ---------------------------------------

const
  BOT_VERSION = '1';
  BOT_RELEASE_DATE = 'Sep 15, 2018';
 
  IP_ADDRESS_REGEXP = '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
                      '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
                      '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' +
                      '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b';
var
  bFlag: boolean;
  sReplyText: string;

function GetGeoIPInfo(sIP: string): boolean;
var
  sCountry, sTown, s: string;
begin
  if RegExpIsMatch(sIP, IP_ADDRESS_REGEXP) then begin
    s := GeoIPGetQuickInfo(sIP);

      if length(s) = 0 then sReplyText := 'IP: ' + sIP + CRLF + 'unknown IP address (maybe local?)'
        else begin
          sCountry := Fetch(s, '|');
          sTown    := s;
     
            if length(sTown) > 0 then sTown := ', ' + sTown;
   
          sReplyText := 'IP: ' + sIP + CRLF + sCountry + sTown;
        end;
       
    result := true;
  end else result := false;
end;

procedure BotHelp;
begin
  sReplyText := '-=Elisa Bot=-' + CRLF +
                'v.' + BOT_VERSION + ' / ' + BOT_RELEASE_DATE + CRLF + CRLF +
                '? - help info about commands' + CRLF +
                '<IP address> - discover a country and city by IPv4 address';
end;

function OnPrivateMessage(iCID, iUIN, iUINTo, iMsgType: integer; sMsg: string): boolean;
var
  sCmd: string;
begin
  bFlag := true;
  sReplyText := '';
 
    // if a recipient isthe  built-in bot Elisa with UIN = 0
    if iUINTo = 0 then begin
      // get the command text
      sCmd := UpperCase(mConvertMsgToPlainText(sMsg, iMsgType));
     
        if GetGeoIPInfo(sCmd) then bFlag := false // if this is an IP address
        else begin
          BotHelp; // show help
          bFlag := false;
        end; 
    end;
 
   // if a command was processed, "kill" the original message
   // and send a new one on behalf of built-in bot Elisa with UIN = 0
   if not bFlag then
     mSendPrivateMessage(0, iUIN, sReplyText, 1);
   
  result := bFlag;
end;

begin

end.

How it looks like in MyChat Client?

elisa-the-bot.png
Elisa replies to user's request about IP address
elisa-the-bot.png (6.93 KiB) Viewed 23434 times
User avatar
Cobus van der Walt
Hi

I attempted to add the script but I get the following error


[12:48:23] (RunError "Ellisabot"): Script operation time: 4 ms
[12:48:23] (RunError "Ellisabot"): [Error] (69:55): Invalid number of parameters
[12:48:28] (RunError "Ellisabot"): Script operation time: 4 ms
[12:48:28] (RunError "Ellisabot"): [Error] (69:55): Invalid number of parameters