From Stealth Client

Jump to: navigation, search

Messengers

Work with bots in Stealth realized with messengers bots technology.

At then moment has been done realization of Telegram and Viber.

'Skype' can't be done due to to technical reson

'Discord' - may be done...sometimes.

In the internal scripts (PS and PAX) there is a TMessenger class

  TMessenger = class
  public
    procedure SendMessage(Msg, UserID : String); virtual;
    property MessengerName: string read GetMessengerName;
    property Connected : Boolean read fConnected write SetConnected default False;
    property Token : String read GetToken write SetToken;
  end;

and 2 variables:

  ViberMessenger : TMessenger;
  TelegramMessenger : TMessenger;

Эти 2 переменных и их свойства видны при отладке.


Token must be set before connection (Bot Registration instruction - Doc:RU/Manual/Reference/Messengers/BotRegistration )


Warning! If Ngrok not installеd, Stealth will offer you to download it and put to Stealth folder, otherwise only Telegram will connect.


WARNING! In external scripts first parameter in evMessengerEvent is not a TMessenger object, but messenger code - 1 is telegram, 2 - viber.

Big example for messenger all-side using

Program New;
 
procedure MessengerEventHandler(Sender : TMessenger; SenderNickName : String; SenderId, ChatId : String; EventMsg : String; EventCode : Byte);
begin
  case EventCode of
    0 : AddToSystemJournal(Sender.MessengerName + ' Connected');
    1 : AddToSystemJournal(Sender.MessengerName + ' Disconnected');
    2 : begin
          AddToSystemJournal(Sender.MessengerName + ' Message incoming from "' + SenderNickName + '" (SenderId: ' + SenderId + ') : ' + EventMsg);
          Sender.SendMessage('response from Stealth : ' + EventMsg,SenderId)           
        end;
    3 : AddToSystemJournal(Sender.MessengerName + ' Error raised : ' + EventMsg);
  end;
end;
 
Begin
  SetEventProc(evMessengerEvent,'MessengerEventHandler');
 
 if not ViberMessenger.Connected then
  begin
    ViberMessenger.Token := 'your_token_here';
    ViberMessenger.Connected := True;
  end;
 
  ViberMessenger.SendMessage('hello from Stealth ViberBot!','receiver_id_here');
 
 
  if not TelegramMessenger.Connected then
  begin
    TelegramMessenger.Token := 'your_token_here';
    TelegramMessenger.Connected := True;
  end;
 
  TelegramMessenger.SendMessage('hello from Stealth TelegramBot!','receiver_id_here');
 
  While True do
  begin
    wait(200);
  end;
 
end.


Example2, external script on Delphi

program Project2;
 
{$R *.res}
 
uses
  System.SysUtils,
  windows,
  stealth_script in '..\..\stealth_script.pas';
 
procedure MessengerEventHandler(MesID : Cardinal; SenderNickName : String;
SenderId, ChatId : String; EventMsg : String; EventCode : Byte);stdcall;
begin
  with Script do
  case MesID of
    0,1 :
    case EventCode of
      0 : AddToSystemJournal('Telegram' + ' Connected');
      1 : AddToSystemJournal('Telegram' + ' Disconnected');
      2 : begin
            AddToSystemJournal('Telegram' + ' Message incoming from "' + SenderNickName + '" (SenderId: ' + SenderId + ') : ' + EventMsg);
            TelegramMessenger.SendMessage('response from Stealth : ' + EventMsg,SenderId)
          end;
      3 : AddToSystemJournal('Telegram' + ' Error raised : ' + EventMsg);
    end;
 
    2:
    case EventCode of
      0 : AddToSystemJournal('Viber' + ' Connected');
      1 : AddToSystemJournal('Viber' + ' Disconnected');
      2 : begin
            AddToSystemJournal('Viber' + ' Message incoming from "' + SenderNickName + '" (SenderId: ' + SenderId + ') : ' + EventMsg);
            ViberMessenger.SendMessage('response from Stealth : ' + EventMsg,SenderId)
          end;
      3 : AddToSystemJournal('Viber' + ' Error raised : ' + EventMsg);
    end;
  end;
end;
 
Begin
  Script.SetEventProc(evMessengerEvent, @MessengerEventHandler);
  Script.ViberMessenger.Connected := False;
 
 if not Script.ViberMessenger.Connected then
  begin
    Script.ViberMessenger.Token := 'your_token_here';
    Script.ViberMessenger.Connected := True;
  end;
 
  Script.ViberMessenger.SendMessage('hello from Stealth ViberBot!','receiver_id_here');
 
 
  if not Script.TelegramMessenger.Connected then
  begin
    Script.TelegramMessenger.Token := 'your_token_here';
    Script.TelegramMessenger.Connected := True;
  end;
 
  Script.TelegramMessenger.SendMessage('hello from Stealth TelegramBot!','receiver_id_here');
 
  While True do
    Script.wait(200);
 
end.

ID of uer-sender can be easy taken - just send any message from this user to our bot, connected by Stealth with script from examples. In Sysjournal will come message like 17:27:37:759 [Test - Test]: Telegram Message incoming from "Vizit0r" (SenderId: 435486649) : testtest where 435486649 is sender ID. (id - fake, given for example).


Example3, external script on Python:

MSG = 'Event test'
ID = 0  # 1-Telegram, 2-Viber, 0-default
TOKEN = 'your_token_here'
USER_ID = 'id'
 
 
def callback(*args):
    AddToSystemJournal('Callback args: ', args)
 
 
def main():
    AddToSystemJournal('Messenger test started')
    SetEventProc('evMessengerEvent', callback)
 
    MessengerSetToken(ID, TOKEN)
    AddToSystemJournal('Token set', MessengerGetToken(ID))
 
    AddToSystemJournal('Connecting...')
    MessengerSetConnected(ID, True)
    while not MessengerGetConnected(ID):
        Wait(100)
    else:
        AddToSystemJournal('Connected state: ', MessengerGetConnected(ID))
 
    AddToSystemJournal('Name is: ', MessengerGetName(ID))
 
    AddToSystemJournal('Sending message to {0}'.format(USER_ID))
    MessengerSendMessage(ID, MSG, USER_ID)
 
    AddToSystemJournal('Disconnecting...')
    MessengerSetConnected(ID, False)
 
    AddToSystemJournal('Done.')
 
 
if __name__ == '__main__':
    main()


Пример 4, external script on C#

Stealth _s = Stealth.Default;
 
_s.Viber.Connected += (o, e) => { _s.Journal.AddToSystemJournal("Viber connected"); };
_s.Viber.Disconnected += (o, e) => { _s.Journal.AddToSystemJournal("Viber disconnected"); };
 
_s.Viber.IncomingMessage += (o, e) =>
{
    _s.Journal.AddToSystemJournal($"Viber incoming from '{e.SenderNickname}' (SenderId: { e.SenderId}): {e.Message}");
    _s.Viber.SendMessage($"Viber response from Stealth: {e.Message}", e.SenderId);
};
 
_s.Viber.Error += (o, e) => { _s.Journal.AddToSystemJournal($"Viber error: {e.Message}"); };
 
_s.Telegram.Connected += (o, e) => { _s.Journal.AddToSystemJournal("Telegram connected"); };
_s.Telegram.Disconnected += (o, e) => { _s.Journal.AddToSystemJournal("Telegram disconnected"); };
_s.Telegram.IncomingMessage += (o, e) =>
{
    _s.Journal.AddToSystemJournal($"Telegram incoming from '{e.SenderNickname}' (SenderId: { e.SenderId}): {e.Message}");
    _s.Telegram.SendMessage($"Telegram response from Stealth: {e.Message}", e.SenderId);
};
_s.Telegram.Error += (o, e) => { _s.Journal.AddToSystemJournal($"Telegram error: {e.Message}"); };
 
_s.Viber.Connect(VIBER_TOKEN);
_s.Telegram.Connect(TELEGRAM_TOKEN);