-
Notifications
You must be signed in to change notification settings - Fork 3
/
TelegramMain.pas
400 lines (361 loc) · 11.9 KB
/
TelegramMain.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
unit TelegramMain;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
FMX.Controls.Presentation, FMX.TabControl, FMX.ListView.Types,
FMX.ListView.Appearances, FMX.ListView.Adapters.Base, FMX.ListView,
Fmx.Bind.GenData, Data.Bind.GenData, Data.Bind.Components,
Data.Bind.ObjectScope,System.Generics.Collections, System.Rtti,
System.Bindings.Outputs, Fmx.Bind.Editors, Data.Bind.EngExt, Fmx.Bind.DBEngExt,
FMX.Objects, FMX.Layouts, FMX.ExtCtrls, sgcBase_Classes,
sgcLib_Telegram_Client, sgcWebSocket_APIs, FMX.Edit, FMX.ScrollBox, FMX.Memo,UChatFrame;
type
TTelegramContact = class(TObject)
public
ContactName:string;
FirstName,LastName:string;
UserName:string;
UserID : Int64;
UserIDstr : string;
ProfilePhotoId:string;
Avatar : TBitmap;
constructor Create();
function GetCivility:string;
end;
type
THeaderFooterForm = class(TForm)
Header: TToolBar;
Footer: TToolBar;
HeaderLabel: TLabel;
TabControl1: TTabControl;
TabItemContacts: TTabItem;
lblHeaderContact: TLabel;
ListViewContacts: TListView;
DataGeneratorAdapterContact: TDataGeneratorAdapter;
BindingsList1: TBindingsList;
RoundRect1: TRoundRect;
IconAddContact: TImage;
sgcTelegram: TsgcTDLib_Telegram;
lblStatus: TLabel;
TabItemAddContact: TTabItem;
Rectangle1: TRectangle;
lblAddContactInfo: TLabel;
edtPhone: TEdit;
lblPlus: TLabel;
TabDebug: TTabItem;
MemoDebug: TMemo;
AGeneratorAdapterContact: TAdapterBindSource;
LinkListControlToField1: TLinkListControlToField;
Button2: TButton;
procedure RoundRect1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure sgcTelegramAuthenticationCode(Sender: TObject; var Code: string);
procedure sgcTelegramAuthorizationStatus(Sender: TObject;
const Status: string);
procedure sgcTelegramException(Sender: TObject; E: Exception);
procedure TabItemAddContactClick(Sender: TObject);
procedure edtPhoneExit(Sender: TObject);
procedure sgcTelegramEvent(Sender: TObject; const Event, Text: string);
procedure ListViewContactsDblClick(Sender: TObject);
procedure AGeneratorAdapterContactCreateAdapter(Sender: TObject;
var ABindSourceAdapter: TBindSourceAdapter);
procedure sgcTelegramNewChat(Sender: TObject; Chat: TsgcTelegramChat);
procedure sgcTelegramMessageText(Sender: TObject;
MessageText: TsgcTelegramMessageText);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FPendingUserID:string;
FUserIDChatFrameDictionnary:TDictionary<string,TChatFrame>;
FChatIDChatFrameDictionnary:TDictionary<string,TChatFrame>;
FChatList:TList<TsgcTelegramChat>;
FContactList : TList<TTelegramContact>;
FFileIDFilePathDico : TDictionary<string,string>;
procedure ParseUpdateFile(Text:string);
procedure ParseFile(Text:string);
procedure ImportContacts(aPhoneNumber:string);
function ParseImportedContacts(aText: string):TStringList;
procedure DownloadFile(fileId:string);
procedure ImportUser(aText:string);
public
{ Public declarations }
end;
var
HeaderFooterForm: THeaderFooterForm;
implementation
uses JSon;
{$R *.fmx}
function THeaderFooterForm.ParseImportedContacts(aText: string):TStringList;
var aJson:TJsonValue;
userIdArray:TJsonArray;
iter:integer;
begin
Result := TStringList.Create;
aJson := TJSONObject.ParseJSONValue(aText);
userIdArray := aJson.P['user_ids'] as TJsonArray;
for iter := 0 to userIdArray.Count-1 do
begin
Result.Add(userIdArray.Items[iter].ToJSON);
end;
aJson.Free;
end;
procedure THeaderFooterForm.ParseUpdateFile(Text: string);
var aJson,afile,aLocal : TJsonValue;
fileid,apath:string;
aTelegram:TTelegramContact;
begin
aJson := TJSONObject.ParseJSONValue(Text);
afile := aJson.FindValue('file');
fileid := afile.P['id'].Value;
aLocal := afile.FindValue('local');
apath := aLocal.P['path'].Value;
if aPath<>'' then
begin
for aTelegram in FContactList do
begin
if aTelegram.ProfilePhotoId=fileid then
begin
aTelegram.Avatar.LoadFromFile(apath);
end;
end;
end;
aJson.Free;
end;
procedure THeaderFooterForm.ParseFile(Text: string);
var aJson,afile,aLocal : TJsonValue;
fileid,apath:string;
aTelegram:TTelegramContact;
begin
aJson := TJSONObject.ParseJSONValue(Text);
fileid := aJson.P['id'].Value;
aLocal := aJson.FindValue('local');
apath := aLocal.P['path'].Value;
if aPath<>'' then
begin
for aTelegram in FContactList do
begin
if aTelegram.ProfilePhotoId=fileid then
begin
AGeneratorAdapterContact.Active := False;
AGeneratorAdapterContact.Adapter.Active := False;
aTelegram.Avatar.LoadFromFile(apath);
AGeneratorAdapterContact.Active := True;
AGeneratorAdapterContact.Adapter.Active := True;
end;
end;
end;
aJson.Free;
end;
procedure THeaderFooterForm.AGeneratorAdapterContactCreateAdapter(
Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter);
begin
FContactList := TList<TTelegramContact>.Create;
ABindSourceAdapter := TListBindSourceAdapter<TTelegramContact>.Create(Self,FContactList,True);
end;
procedure THeaderFooterForm.Button2Click(Sender: TObject);
begin
TabControl1.ActiveTab := TabItemContacts;
end;
procedure THeaderFooterForm.DownloadFile(fileId: string);
var aDownload:string;
begin
aDownload := '{"@type": "downloadFile","file_id":"'+fileId+'","priority":"1"}';
MemoDebug.Lines.Add('Request:'+aDownload);
sgcTelegram.TDLibSend(aDownload);
end;
procedure THeaderFooterForm.edtPhoneExit(Sender: TObject);
begin
ImportContacts(edtPhone.Text);
end;
procedure THeaderFooterForm.FormCreate(Sender: TObject);
var aConfigFile:TStringList;
begin
FFileIDFilePathDico := TDictionary<string,string>.Create;
FUserIDChatFrameDictionnary:=TDictionary<string,TChatFrame>.Create;
FChatIDChatFrameDictionnary:=TDictionary<string,TChatFrame>.Create;
FChatList:=TList<TsgcTelegramChat>.Create;
aConfigFile:= TStringList.Create;
aConfigFile.LoadFromFile('config.ini');
sgcTelegram.Telegram.API.ApiId := Trim(aConfigFile.Values['api_id']);
sgcTelegram.Telegram.API.ApiHash := Trim(aConfigFile.Values['api_hash']);
sgcTelegram.Telegram.PhoneNumber := Trim(aConfigFile.Values['phone']);
sgcTelegram.Telegram.Parameters.ApplicationVersion := '1.0';
sgcTelegram.Telegram.Parameters.DeviceModel := 'Desktop';
sgcTelegram.Telegram.Parameters.LanguageCode := 'en';
sgcTelegram.Telegram.Parameters.SystemVersion := 'Windows';
if (sgcTelegram.Telegram.API.ApiId='') or
(sgcTelegram.Telegram.API.ApiHash ='') or
(sgcTelegram.Telegram.PhoneNumber ='') then
begin
ShowMessage('Please fill the config.ini file');
end
else
begin
sgcTelegram.Active := True;
end;
aConfigFile.Free;
// TabControl1.TabPosition := TTabPosition.None;
end;
procedure THeaderFooterForm.ImportContacts(aPhoneNumber:string);
begin
sgcTelegram.TDLibSend('{"@type": "importContacts", "contacts": [{"phone_number": "'+aPhoneNumber+'"}]}');
end;
procedure THeaderFooterForm.ImportUser(aText: string);
var aJson,anUSer,profilePhoto,asmall:TJsonValue;
aTelegramContact:TTelegramContact;
begin
AGeneratorAdapterContact.Active := False;
AGeneratorAdapterContact.Adapter.Active := False;
aJson := TJSONObject.ParseJSONValue(aText);
anUser := aJson.FindValue('user'); //.GetValue('user');
aTelegramContact:=TTelegramContact.Create;
aTelegramContact.UserID := StrToInt64(anUser.P['id'].Value);
aTelegramContact.UserIDStr := anUser.P['id'].Value;
aTelegramContact.UserName := anUser.P['username'].Value ;
aTelegramContact.FirstName := anUser.P['first_name'].Value;
aTelegramContact.LastName := anUser.P['last_name'].Value;
profilePhoto := anUser.FindValue('profile_photo');
if profilePhoto<>nil then
begin
asmall := profilePhoto.FindValue('small');
if asmall<>nil then
begin
aTelegramContact.ProfilePhotoId := asmall.P['id'].Value;
FFileIDFilePathDico.add(aTelegramContact.ProfilePhotoId,'');
DownloadFile(aTelegramContact.ProfilePhotoId);
end;
end;
aTelegramContact.ContactName := aTelegramContact.UserName;
if aTelegramContact.ContactName<>'' then
begin
if Trim(aTelegramContact.GetCivility)<>'' then
aTelegramContact.ContactName := aTelegramContact.ContactName +'( '+aTelegramContact.GetCivility+' )';
end
else
begin
aTelegramContact.ContactName := aTelegramContact.GetCivility;
end;
FContactList.Add(aTelegramContact);
AGeneratorAdapterContact.Active := True;
AGeneratorAdapterContact.Adapter.Active := True;
TabControl1.ActiveTab := TabItemContacts;
end;
procedure THeaderFooterForm.ListViewContactsDblClick(Sender: TObject);
var anItem:TListViewItem;
contactName:string;
UserID:InT64;
aFrame:TChatFrame;
begin
if FPendingUserID='' then
begin
anItem := ListViewContacts.Selected as TListViewItem;
UserID := StrTOInt64DEf(anItem.Detail,0);
if FUserIDChatFrameDictionnary.TryGetValue(anItem.Detail,aFrame) then
begin
if aFrame<>nil then
begin
TabControl1.ActiveTab := aFrame.TabItemParent;
end;
end
else
begin
contactName := anItem.Text;
if UserID>0 then
begin
FPendingUserID := UserID.ToString;
sgcTelegram.CreateNewBasicGroupChat([UserID],contactName);
end;
end;
end;
end;
procedure THeaderFooterForm.RoundRect1Click(Sender: TObject);
begin
TabControl1.ActiveTab := TabItemAddContact;
end;
procedure THeaderFooterForm.sgcTelegramAuthenticationCode(Sender: TObject;
var Code: string);
begin
Code := InputBox('Telegram Delphi','Please enter code','');
end;
procedure THeaderFooterForm.sgcTelegramAuthorizationStatus(Sender: TObject;
const Status: string);
begin
lblStatus.Text := Status;
if Status = 'authorizationStateReady' then
begin
ImportContacts(''); //start the get of contcats
end;
end;
procedure THeaderFooterForm.sgcTelegramEvent(Sender: TObject; const Event,
Text: string);
var aUserList:TStringList;
anuserId:integer;
begin
MemoDebug.Lines.Add(Text);
if Event ='importedContacts' then
begin
aUserList := ParseImportedContacts(Text);
anuserId := strtointDef(aUserList[0],0);
if anuserId>0 then
sgcTelegram.GetUser(anuserId); //this method will raise a TDLib event updateUser
end;
if (Event = 'updateUser') then
begin
ImportUser(Text);
end;
if (Event = 'updateFile') then
begin
ParseUpdateFile(Text);
end;
if (Event ='file') then
begin
ParseFile(Text);
end;
end;
procedure THeaderFooterForm.sgcTelegramException(Sender: TObject; E: Exception);
begin
ShowMessage(E.Message);
end;
procedure THeaderFooterForm.sgcTelegramMessageText(Sender: TObject;
MessageText: TsgcTelegramMessageText);
var achatFrame:TChatFrame;
senderText:string;
begin
FChatIDChatFrameDictionnary.TryGetValue(MessageText.ChatId,achatFrame);
if achatFrame<>nil then
begin
senderText := '';
if sgcTelegram.MyId <> MessageText.SenderUserId then
senderText := inttostr(MessageText.SenderUserId);
achatFrame.AddMessage(MessageText.Text,senderText);
end;
end;
procedure THeaderFooterForm.sgcTelegramNewChat(Sender: TObject;
Chat: TsgcTelegramChat);
var aTabItem:TTabItem;
aChatFrame:TChatFrame;
begin
aTabItem := TabControl1.Add;
aTabItem.Text := Chat.Title;
aChatFrame:=TChatFrame.Create(sgcTelegram, aTabItem,Chat.Title,Chat.ChatId);
aChatFrame.Parent := aTabItem;
if FPendingUserID<>'' then
FUserIDChatFrameDictionnary.TryAdd(FPendingUserID,aChatFrame);
FChatIDChatFrameDictionnary.TryAdd(Chat.ChatId,aChatFrame);
FPendingUserID :='';
TabControl1.ActiveTab := aTabItem;
end;
procedure THeaderFooterForm.TabItemAddContactClick(Sender: TObject);
begin
end;
{ TTelegramContact }
constructor TTelegramContact.Create;
begin
Avatar := TBitmap.Create;
end;
function TTelegramContact.GetCivility: string;
begin
Result := FirstName +' '+LastName;
end;
end.