@@ -7,50 +7,50 @@ interface
77 System.Types,
88 System.UITypes,
99 System.Classes,
10- System.Variants,
1110 FMX.Types,
12- FMX.Controls,
1311 FMX.Forms,
14- FMX.Graphics,
1512 FMX.Dialogs,
16- FMX.Controls.Presentation,
1713 FMX.Memo,
18- FMX.ScrollBox,
1914 TelegAPI.Bot,
2015 TelegAPI.Types,
2116 TelegAPI.Exceptions,
17+ FMX.Edit,
2218 FMX.StdCtrls,
23- FMX.Edit;
19+ FMX.Controls,
20+ FMX.Controls.Presentation,
21+ FMX.ScrollBox,
22+ FMX.Layouts;
2423
2524type
2625 TMain = class (TForm)
2726 tgBot: TTelegramBot;
28- mmo1: TMemo;
29- edt1: TEdit;
30- btn1: TEditButton;
31- procedure FormCreate (Sender: TObject);
27+ mmoLog: TMemo;
28+ Layout1: TLayout;
29+ lblToken: TLabel;
30+ edtToken: TEdit;
31+ swtchToken: TSwitch;
3232 procedure tgBotInlineResultChosen (ASender: TObject; AChosenInlineResult: TtgChosenInlineResult);
3333 procedure tgBotInlineQuery (ASender: TObject; AInlineQuery: TtgInlineQuery);
34- procedure tgBotMessage (ASender: TObject; AMessage: TtgMessage );
34+ procedure tgBotMessage (ASender: TObject; AMessage: TTgMessage );
3535 procedure tgBotCallbackQuery (ASender: TObject; ACallbackQuery: TtgCallbackQuery);
3636 procedure tgBotReceiveError (ASender: TObject; AApiRequestException: EApiRequestException);
3737 procedure tgBotConnect (Sender: TObject);
3838 procedure tgBotDisconnect (Sender: TObject);
3939 procedure FormClose (Sender: TObject; var Action: TCloseAction);
4040 procedure tgBotReceiveGeneralError (ASender: TObject; AException: Exception);
41- procedure btn1Click (Sender: TObject);
41+ procedure swtchTokenSwitch (Sender: TObject);
4242 private
4343 { Private declarations }
4444 procedure WriteLine (const AValue: string);
45- procedure SendInline (Msg: TtgMessage );
46- procedure SendKeyboard (Msg: TtgMessage );
47- procedure SendPhoto (Msg: TtgMessage );
48- procedure SendRequest (Msg: TtgMessage );
49- procedure SendQuest (Msg: TtgMessage );
45+ procedure SendInline (Msg: TTgMessage );
46+ procedure SendKeyboard (Msg: TTgMessage );
47+ procedure SendPhoto (Msg: TTgMessage );
48+ procedure SendRequest (Msg: TTgMessage );
49+ procedure SendQuest (Msg: TTgMessage );
5050 // parsing
51- procedure ParseTextMessage (Msg: TtgMessage );
52- procedure ParsePhotoMessage (Msg: TtgMessage );
53- procedure ParseLocationMessage (Msg: TtgMessage );
51+ procedure ParseTextMessage (Msg: TTgMessage );
52+ procedure ParsePhotoMessage (Msg: TTgMessage );
53+ procedure ParseLocationMessage (Msg: TTgMessage );
5454 public
5555 { Public declarations }
5656 end ;
@@ -69,35 +69,17 @@ implementation
6969 TelegAPI.Types.InputMessageContents;
7070{ $R *.fmx}
7171
72- procedure TMain.btn1Click (Sender: TObject);
73- var
74- LJson: string;
75- LReturn: TArray<TtgUpdate>;
76- begin
77- LJson := TFile.ReadAllText(edt1.Text, TEncoding.UTF8);
78- LReturn := tgBot.ApiTest<TArray<TtgUpdate>>(LJson);
79- end ;
80-
8172procedure TMain.FormClose (Sender: TObject; var Action: TCloseAction);
8273begin
8374 tgBot.IsReceiving := False;
8475end ;
8576
86- procedure TMain.FormCreate (Sender: TObject);
87- begin
88- ReportMemoryLeaksOnShutdown := True;
89- tgBot.Token := { $I ..\token.inc} ;
90- if not tgBot.IsValidToken then
91- raise ELoginCredentialError.Create(' invalid token format' );
92- tgBot.IsReceiving := True;
93- end ;
94-
95- procedure TMain.ParseLocationMessage (Msg: TtgMessage);
77+ procedure TMain.ParseLocationMessage (Msg: TTgMessage);
9678begin
9779 WriteLine(' Location: ' + Msg.Location.Longitude.ToString + ' ' + Msg.Location.Latitude.ToString);
9880end ;
9981
100- procedure TMain.ParsePhotoMessage (Msg: TtgMessage );
82+ procedure TMain.ParsePhotoMessage (Msg: TTgMessage );
10183var
10284 LFile: TtgFile;
10385begin
@@ -111,7 +93,7 @@ procedure TMain.ParsePhotoMessage(Msg: TtgMessage);
11193 end ;
11294end ;
11395
114- procedure TMain.ParseTextMessage (Msg: TtgMessage );
96+ procedure TMain.ParseTextMessage (Msg: TTgMessage );
11597var
11698 usage: string;
11799begin
@@ -147,7 +129,7 @@ procedure TMain.ParseTextMessage(Msg: TtgMessage);
147129 end ;
148130end ;
149131
150- procedure TMain.SendRequest (Msg: TtgMessage );
132+ procedure TMain.SendRequest (Msg: TTgMessage );
151133var
152134 kb: IReplyMarkup;
153135begin
@@ -157,13 +139,23 @@ procedure TMain.SendRequest(Msg: TtgMessage);
157139 tgBot.SendMessage(Msg.Chat.Id, ' Who or Where are you?' , TtgParseMode.default, False, False, 0 , kb).Free;
158140end ;
159141
160- procedure TMain.SendPhoto (Msg: TtgMessage);
142+ procedure TMain.swtchTokenSwitch (Sender: TObject);
143+ begin
144+ tgBot.Token := edtToken.Text;
145+ if not tgBot.IsValidToken then
146+ raise ELoginCredentialError.Create(' invalid token format' );
147+ tgBot.IsReceiving := swtchToken.IsChecked;
148+ end ;
149+
150+ procedure TMain.SendPhoto (Msg: TTgMessage);
161151const
162152 PATH_PHOTO = ' C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg' ;
163153var
164154 LFile: TtgFileToSend;
165155begin
166156 tgBot.SendChatAction(Msg.Chat.Id, TtgSendChatAction.UploadPhoto);
157+ if not TFile.Exists(PATH_PHOTO) then
158+ WriteLine(' Change path to photo in metod: TMain.SendPhoto' );
167159 LFile := TtgFileToSend.Create(PATH_PHOTO);
168160 try
169161 tgBot.SendPhoto(Msg.Chat.ID, LFile, ' Nice Picture' ).Free;
@@ -186,7 +178,7 @@ procedure TMain.SendInline;
186178 tgBot.SendMessage(Msg.Chat.Id, ' Choose' , TtgParseMode.default, False, False, 0 , keyboard).Free;
187179end ;
188180
189- procedure TMain.SendKeyboard (Msg: TtgMessage );
181+ procedure TMain.SendKeyboard (Msg: TTgMessage );
190182var
191183 keyboard: IReplyMarkup;
192184begin
@@ -258,7 +250,7 @@ procedure TMain.tgBotInlineQuery(ASender: TObject; AInlineQuery: TtgInlineQuery)
258250 tgBot.AnswerInlineQuery(AInlineQuery.Id, results, 0 , True);
259251end ;
260252
261- procedure TMain.SendQuest (Msg: TtgMessage );
253+ procedure TMain.SendQuest (Msg: TTgMessage );
262254var
263255 keyboard: IReplyMarkup;
264256begin
@@ -275,7 +267,7 @@ procedure TMain.tgBotInlineResultChosen(ASender: TObject; AChosenInlineResult: T
275267 WriteLine(' Received choosen inline result: ' + AChosenInlineResult.ResultId);
276268end ;
277269
278- procedure TMain.tgBotMessage (ASender: TObject; AMessage: TtgMessage );
270+ procedure TMain.tgBotMessage (ASender: TObject; AMessage: TTgMessage );
279271begin
280272 case AMessage.&Type of
281273 TtgMessageType.TextMessage:
@@ -289,15 +281,7 @@ procedure TMain.tgBotMessage(ASender: TObject; AMessage: TtgMessage);
289281
290282procedure TMain.tgBotReceiveError (ASender: TObject; AApiRequestException: EApiRequestException);
291283begin
292- case AApiRequestException.ErrorCode of
293- 401 :
294- begin
295- tgBot.IsReceiving := False;
296- ShowMessage(AApiRequestException.Message);
297- end ;
298- end ;
299284 WriteLine(AApiRequestException.ToString);
300- AApiRequestException.Free;
301285end ;
302286
303287procedure TMain.tgBotReceiveGeneralError (ASender: TObject; AException: Exception);
@@ -307,8 +291,16 @@ procedure TMain.tgBotReceiveGeneralError(ASender: TObject; AException: Exception
307291
308292procedure TMain.WriteLine (const AValue: string);
309293begin
310- mmo1.Lines.Insert(0 , AValue);
294+ mmoLog.Lines.Add(AValue);
295+ mmoLog.ScrollBy(0 , mmoLog.ContentBounds.Bottom, False);
311296end ;
312297
298+ initialization
299+ ReportMemoryLeaksOnShutdown := True;
300+
301+ finalization
302+ if ReportMemoryLeaksOnShutdown then
303+ CheckSynchronize();
304+
313305end .
314306
0 commit comments