├── .gitignore
├── LICENSE
├── README.md
├── sample
├── apiExample.dpr
├── apiExample.dproj
├── apiExample.res
├── uFrmMain.dfm
└── uFrmMain.pas
└── src
├── uClassMessageDTO.pas
├── uConsts.pas
├── uTelegramAPI.Interfaces.pas
└── uTelegramAPI.pas
/.gitignore:
--------------------------------------------------------------------------------
1 | # Uncomment these types if you want even more clean repository. But be careful.
2 | # It can make harm to an existing project source. Read explanations below.
3 | #
4 | # Resource files are binaries containing manifest, project icon and version info.
5 | # They can not be viewed as text or compared by diff-tools. Consider replacing them with .rc files.
6 | *.res
7 | #
8 | # Type library file (binary). In old Delphi versions it should be stored.
9 | # Since Delphi 2009 it is produced from .ridl file and can safely be ignored.
10 | #*.tlb
11 | #
12 | # Diagram Portfolio file. Used by the diagram editor up to Delphi 7.
13 | # Uncomment this if you are not using diagrams or use newer Delphi version.
14 | #*.ddp
15 | #
16 | # Visual LiveBindings file. Added in Delphi XE2.
17 | # Uncomment this if you are not using LiveBindings Designer.
18 | #*.vlb
19 | #
20 | # Deployment Manager configuration file for your project. Added in Delphi XE2.
21 | # Uncomment this if it is not mobile development and you do not use remote debug feature.
22 | #*.deployproj
23 | #
24 | # C++ object files produced when C/C++ Output file generation is configured.
25 | # Uncomment this if you are not using external objects (zlib library for example).
26 | #*.obj
27 | #
28 |
29 | # Delphi compiler-generated binaries (safe to delete)
30 | *.exe
31 | *.dll
32 | *.bpl
33 | *.bpi
34 | *.dcp
35 | *.so
36 | *.apk
37 | *.drc
38 | *.map
39 | *.dres
40 | *.rsm
41 | *.tds
42 | *.dcu
43 | *.lib
44 | *.a
45 | *.o
46 | *.ocx
47 |
48 | # Delphi autogenerated files (duplicated info)
49 | *.cfg
50 | *.hpp
51 | *Resource.rc
52 |
53 | # Delphi local files (user-specific info)
54 | *.local
55 | *.identcache
56 | *.projdata
57 | *.tvsconfig
58 | *.dsk
59 |
60 | # Delphi history and backups
61 | __history/
62 | __recovery/
63 | *.~*
64 |
65 | # Castalia statistics file (since XE7 Castalia is distributed with Delphi)
66 | *.stat
67 |
68 | # Boss dependency manager vendor folder https://github.com/HashLoad/boss
69 | modules/
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # telegram-bot-delphi
3 |
4 | ### Features
5 |
6 | - ✅ Send text message
7 | - ✅ Send file
8 | - ✅ Send location
9 | - ✅ Send message with buttons
10 | - ✅ Get chat updates
11 |
12 |
13 |
14 | ### How to create a bot in Telegram?
15 | 1. Start a new chat with [@BotFather](https://t.me/botfather) in Telegram
16 | 2. Send [/newbot]()
17 | 3. Choose a name for your bot
18 | 4. Choose a username, it must end in "bot", Example: tetris_bot
19 | 5. Get and save the "token" to use in examples
20 |
21 |
22 | [Telegram API Documentation](https://core.telegram.org/bots/api)
23 |
--------------------------------------------------------------------------------
/sample/apiExample.dpr:
--------------------------------------------------------------------------------
1 | program apiExample;
2 |
3 | uses
4 | Vcl.Forms,
5 | uFrmMain in 'uFrmMain.pas' {Form1},
6 | uClassMessageDTO in '..\src\uClassMessageDTO.pas',
7 | uConsts in '..\src\uConsts.pas',
8 | uTelegramAPI.Interfaces in '..\src\uTelegramAPI.Interfaces.pas',
9 | uTelegramAPI in '..\src\uTelegramAPI.pas';
10 |
11 | {$R *.res}
12 |
13 | begin
14 | Application.Initialize;
15 | Application.MainFormOnTaskbar := True;
16 | Application.CreateForm(TForm1, Form1);
17 | Application.Run;
18 |
19 | end.
20 |
--------------------------------------------------------------------------------
/sample/apiExample.dproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | {50B1405F-DB48-43FE-9630-EE9555FBB9AB}
4 | 19.2
5 | VCL
6 | apiExample.dpr
7 | True
8 | Release
9 | Win32
10 | 1
11 | Application
12 |
13 |
14 | true
15 |
16 |
17 | true
18 | Base
19 | true
20 |
21 |
22 | true
23 | Base
24 | true
25 |
26 |
27 | true
28 | Base
29 | true
30 |
31 |
32 | true
33 | Cfg_1
34 | true
35 | true
36 |
37 |
38 | true
39 | Base
40 | true
41 |
42 |
43 | true
44 | Cfg_2
45 | true
46 | true
47 |
48 |
49 | .\$(Platform)\$(Config)
50 | .\$(Platform)\$(Config)
51 | false
52 | false
53 | false
54 | false
55 | false
56 | System;Xml;Data;Datasnap;Web;Soap;Vcl;Vcl.Imaging;Vcl.Touch;Vcl.Samples;Vcl.Shell;$(DCC_Namespace)
57 | $(BDS)\bin\delphi_PROJECTICON.ico
58 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png
59 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png
60 | apiExample
61 | .\..\src;$(DCC_UnitSearchPath)
62 | 1046
63 | CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=
64 |
65 |
66 | DBXSqliteDriver;RESTComponents;bindcompdbx;IndyIPCommon;DBXInterBaseDriver;vcl;IndyIPServer;vclactnband;vclFireDAC;IndySystem;tethering;svnui;dsnapcon;FireDACADSDriver;FireDACMSAccDriver;fmxFireDAC;vclimg;FireDAC;vcltouch;vcldb;bindcompfmx;svn;FireDACSqliteDriver;FireDACPgDriver;inetdb;CEF4Delphi;soaprtl;DbxCommonDriver;fmx;FireDACIBDriver;fmxdae;xmlrtl;soapmidas;fmxobj;vclwinx;rtl;DbxClientDriver;CustomIPTransport;vcldsnap;dbexpress;IndyCore;vclx;bindcomp;appanalytics;dsnap;FireDACCommon;IndyIPClient;bindcompvcl;RESTBackendComponents;VCLRESTComponents;soapserver;dbxcds;VclSmp;adortl;vclie;bindengine;DBXMySQLDriver;CloudService;dsnapxml;FireDACMySQLDriver;dbrtl;IndyProtocols;inetdbxpress;FireDACCommonODBC;FireDACCommonDriver;inet;fmxase;$(DCC_UsePackage)
67 | Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace)
68 | Debug
69 | true
70 | CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=
71 | 1033
72 | $(BDS)\bin\default_app.manifest
73 |
74 |
75 | DBXSqliteDriver;RESTComponents;bindcompdbx;IndyIPCommon;DBXInterBaseDriver;vcl;IndyIPServer;vclactnband;vclFireDAC;IndySystem;tethering;dsnapcon;FireDACADSDriver;FireDACMSAccDriver;fmxFireDAC;vclimg;FireDAC;vcltouch;vcldb;bindcompfmx;FireDACSqliteDriver;FireDACPgDriver;inetdb;CEF4Delphi;soaprtl;DbxCommonDriver;fmx;FireDACIBDriver;fmxdae;xmlrtl;soapmidas;fmxobj;vclwinx;rtl;DbxClientDriver;CustomIPTransport;vcldsnap;dbexpress;IndyCore;vclx;bindcomp;appanalytics;dsnap;FireDACCommon;IndyIPClient;bindcompvcl;RESTBackendComponents;VCLRESTComponents;soapserver;dbxcds;VclSmp;adortl;vclie;bindengine;DBXMySQLDriver;CloudService;dsnapxml;FireDACMySQLDriver;dbrtl;IndyProtocols;inetdbxpress;FireDACCommonODBC;FireDACCommonDriver;inet;fmxase;$(DCC_UsePackage)
76 |
77 |
78 | DEBUG;$(DCC_Define)
79 | true
80 | false
81 | true
82 | true
83 | true
84 |
85 |
86 | false
87 | true
88 | PerMonitorV2
89 |
90 |
91 | false
92 | RELEASE;$(DCC_Define)
93 | 0
94 | 0
95 |
96 |
97 | true
98 | PerMonitorV2
99 | true
100 | 1033
101 |
102 |
103 |
104 | MainSource
105 |
106 |
107 |
108 | dfm
109 |
110 |
111 |
112 |
113 |
114 |
115 | Cfg_2
116 | Base
117 |
118 |
119 | Base
120 |
121 |
122 | Cfg_1
123 | Base
124 |
125 |
126 |
127 | Delphi.Personality.12
128 | Application
129 |
130 |
131 |
132 | apiExample.dpr
133 |
134 |
135 | Microsoft Office 2000 Sample Automation Server Wrapper Components
136 | Microsoft Office XP Sample Automation Server Wrapper Components
137 |
138 |
139 |
140 |
141 |
142 | apiExample.exe
143 | true
144 |
145 |
146 |
147 |
148 | apiExample.exe
149 | true
150 |
151 |
152 |
153 |
154 | 1
155 |
156 |
157 | Contents\MacOS
158 | 1
159 |
160 |
161 | 0
162 |
163 |
164 |
165 |
166 | classes
167 | 1
168 |
169 |
170 | classes
171 | 1
172 |
173 |
174 |
175 |
176 | res\xml
177 | 1
178 |
179 |
180 | res\xml
181 | 1
182 |
183 |
184 |
185 |
186 | library\lib\armeabi-v7a
187 | 1
188 |
189 |
190 |
191 |
192 | library\lib\armeabi
193 | 1
194 |
195 |
196 | library\lib\armeabi
197 | 1
198 |
199 |
200 |
201 |
202 | library\lib\armeabi-v7a
203 | 1
204 |
205 |
206 |
207 |
208 | library\lib\mips
209 | 1
210 |
211 |
212 | library\lib\mips
213 | 1
214 |
215 |
216 |
217 |
218 | library\lib\armeabi-v7a
219 | 1
220 |
221 |
222 | library\lib\arm64-v8a
223 | 1
224 |
225 |
226 |
227 |
228 | library\lib\armeabi-v7a
229 | 1
230 |
231 |
232 |
233 |
234 | res\drawable
235 | 1
236 |
237 |
238 | res\drawable
239 | 1
240 |
241 |
242 |
243 |
244 | res\values
245 | 1
246 |
247 |
248 | res\values
249 | 1
250 |
251 |
252 |
253 |
254 | res\values-v21
255 | 1
256 |
257 |
258 | res\values-v21
259 | 1
260 |
261 |
262 |
263 |
264 | res\values
265 | 1
266 |
267 |
268 | res\values
269 | 1
270 |
271 |
272 |
273 |
274 | res\drawable
275 | 1
276 |
277 |
278 | res\drawable
279 | 1
280 |
281 |
282 |
283 |
284 | res\drawable-xxhdpi
285 | 1
286 |
287 |
288 | res\drawable-xxhdpi
289 | 1
290 |
291 |
292 |
293 |
294 | res\drawable-xxxhdpi
295 | 1
296 |
297 |
298 | res\drawable-xxxhdpi
299 | 1
300 |
301 |
302 |
303 |
304 | res\drawable-ldpi
305 | 1
306 |
307 |
308 | res\drawable-ldpi
309 | 1
310 |
311 |
312 |
313 |
314 | res\drawable-mdpi
315 | 1
316 |
317 |
318 | res\drawable-mdpi
319 | 1
320 |
321 |
322 |
323 |
324 | res\drawable-hdpi
325 | 1
326 |
327 |
328 | res\drawable-hdpi
329 | 1
330 |
331 |
332 |
333 |
334 | res\drawable-xhdpi
335 | 1
336 |
337 |
338 | res\drawable-xhdpi
339 | 1
340 |
341 |
342 |
343 |
344 | res\drawable-mdpi
345 | 1
346 |
347 |
348 | res\drawable-mdpi
349 | 1
350 |
351 |
352 |
353 |
354 | res\drawable-hdpi
355 | 1
356 |
357 |
358 | res\drawable-hdpi
359 | 1
360 |
361 |
362 |
363 |
364 | res\drawable-xhdpi
365 | 1
366 |
367 |
368 | res\drawable-xhdpi
369 | 1
370 |
371 |
372 |
373 |
374 | res\drawable-xxhdpi
375 | 1
376 |
377 |
378 | res\drawable-xxhdpi
379 | 1
380 |
381 |
382 |
383 |
384 | res\drawable-xxxhdpi
385 | 1
386 |
387 |
388 | res\drawable-xxxhdpi
389 | 1
390 |
391 |
392 |
393 |
394 | res\drawable-small
395 | 1
396 |
397 |
398 | res\drawable-small
399 | 1
400 |
401 |
402 |
403 |
404 | res\drawable-normal
405 | 1
406 |
407 |
408 | res\drawable-normal
409 | 1
410 |
411 |
412 |
413 |
414 | res\drawable-large
415 | 1
416 |
417 |
418 | res\drawable-large
419 | 1
420 |
421 |
422 |
423 |
424 | res\drawable-xlarge
425 | 1
426 |
427 |
428 | res\drawable-xlarge
429 | 1
430 |
431 |
432 |
433 |
434 | res\values
435 | 1
436 |
437 |
438 | res\values
439 | 1
440 |
441 |
442 |
443 |
444 | 1
445 |
446 |
447 | Contents\MacOS
448 | 1
449 |
450 |
451 | 0
452 |
453 |
454 |
455 |
456 | Contents\MacOS
457 | 1
458 | .framework
459 |
460 |
461 | Contents\MacOS
462 | 1
463 | .framework
464 |
465 |
466 | 0
467 |
468 |
469 |
470 |
471 | 1
472 | .dylib
473 |
474 |
475 | 1
476 | .dylib
477 |
478 |
479 | 1
480 | .dylib
481 |
482 |
483 | Contents\MacOS
484 | 1
485 | .dylib
486 |
487 |
488 | Contents\MacOS
489 | 1
490 | .dylib
491 |
492 |
493 | 0
494 | .dll;.bpl
495 |
496 |
497 |
498 |
499 | 1
500 | .dylib
501 |
502 |
503 | 1
504 | .dylib
505 |
506 |
507 | 1
508 | .dylib
509 |
510 |
511 | Contents\MacOS
512 | 1
513 | .dylib
514 |
515 |
516 | Contents\MacOS
517 | 1
518 | .dylib
519 |
520 |
521 | 0
522 | .bpl
523 |
524 |
525 |
526 |
527 | 0
528 |
529 |
530 | 0
531 |
532 |
533 | 0
534 |
535 |
536 | 0
537 |
538 |
539 | 0
540 |
541 |
542 | Contents\Resources\StartUp\
543 | 0
544 |
545 |
546 | Contents\Resources\StartUp\
547 | 0
548 |
549 |
550 | 0
551 |
552 |
553 |
554 |
555 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
556 | 1
557 |
558 |
559 |
560 |
561 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
562 | 1
563 |
564 |
565 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
566 | 1
567 |
568 |
569 |
570 |
571 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
572 | 1
573 |
574 |
575 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
576 | 1
577 |
578 |
579 |
580 |
581 | 1
582 |
583 |
584 | 1
585 |
586 |
587 | 1
588 |
589 |
590 |
591 |
592 | 1
593 |
594 |
595 | 1
596 |
597 |
598 | 1
599 |
600 |
601 |
602 |
603 | 1
604 |
605 |
606 | 1
607 |
608 |
609 | 1
610 |
611 |
612 |
613 |
614 | 1
615 |
616 |
617 | 1
618 |
619 |
620 | 1
621 |
622 |
623 |
624 |
625 | 1
626 |
627 |
628 | 1
629 |
630 |
631 | 1
632 |
633 |
634 |
635 |
636 | 1
637 |
638 |
639 | 1
640 |
641 |
642 | 1
643 |
644 |
645 |
646 |
647 | 1
648 |
649 |
650 | 1
651 |
652 |
653 | 1
654 |
655 |
656 |
657 |
658 | 1
659 |
660 |
661 | 1
662 |
663 |
664 | 1
665 |
666 |
667 |
668 |
669 | 1
670 |
671 |
672 | 1
673 |
674 |
675 | 1
676 |
677 |
678 |
679 |
680 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
681 | 1
682 |
683 |
684 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
685 | 1
686 |
687 |
688 |
689 |
690 | 1
691 |
692 |
693 | 1
694 |
695 |
696 | 1
697 |
698 |
699 |
700 |
701 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
702 | 1
703 |
704 |
705 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
706 | 1
707 |
708 |
709 |
710 |
711 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
712 | 1
713 |
714 |
715 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
716 | 1
717 |
718 |
719 |
720 |
721 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
722 | 1
723 |
724 |
725 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
726 | 1
727 |
728 |
729 |
730 |
731 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
732 | 1
733 |
734 |
735 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
736 | 1
737 |
738 |
739 |
740 |
741 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
742 | 1
743 |
744 |
745 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
746 | 1
747 |
748 |
749 |
750 |
751 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
752 | 1
753 |
754 |
755 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
756 | 1
757 |
758 |
759 |
760 |
761 | 1
762 |
763 |
764 | 1
765 |
766 |
767 | 1
768 |
769 |
770 |
771 |
772 | 1
773 |
774 |
775 | 1
776 |
777 |
778 | 1
779 |
780 |
781 |
782 |
783 | 1
784 |
785 |
786 | 1
787 |
788 |
789 | 1
790 |
791 |
792 |
793 |
794 | 1
795 |
796 |
797 | 1
798 |
799 |
800 | 1
801 |
802 |
803 |
804 |
805 | 1
806 |
807 |
808 | 1
809 |
810 |
811 | 1
812 |
813 |
814 |
815 |
816 | 1
817 |
818 |
819 | 1
820 |
821 |
822 | 1
823 |
824 |
825 |
826 |
827 | 1
828 |
829 |
830 | 1
831 |
832 |
833 | 1
834 |
835 |
836 |
837 |
838 | 1
839 |
840 |
841 | 1
842 |
843 |
844 | 1
845 |
846 |
847 |
848 |
849 | 1
850 |
851 |
852 | 1
853 |
854 |
855 | 1
856 |
857 |
858 |
859 |
860 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
861 | 1
862 |
863 |
864 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
865 | 1
866 |
867 |
868 |
869 |
870 | 1
871 |
872 |
873 | 1
874 |
875 |
876 | 1
877 |
878 |
879 |
880 |
881 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
882 | 1
883 |
884 |
885 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
886 | 1
887 |
888 |
889 |
890 |
891 | 1
892 |
893 |
894 | 1
895 |
896 |
897 | 1
898 |
899 |
900 |
901 |
902 | 1
903 |
904 |
905 | 1
906 |
907 |
908 | 1
909 |
910 |
911 |
912 |
913 | 1
914 |
915 |
916 | 1
917 |
918 |
919 | 1
920 |
921 |
922 |
923 |
924 | 1
925 |
926 |
927 | 1
928 |
929 |
930 | 1
931 |
932 |
933 |
934 |
935 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
936 | 1
937 |
938 |
939 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
940 | 1
941 |
942 |
943 |
944 |
945 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
946 | 1
947 |
948 |
949 | ..\$(PROJECTNAME).launchscreen\Assets\LaunchScreenImage.imageset
950 | 1
951 |
952 |
953 |
954 |
955 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
956 | 1
957 |
958 |
959 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
960 | 1
961 |
962 |
963 |
964 |
965 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
966 | 1
967 |
968 |
969 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
970 | 1
971 |
972 |
973 |
974 |
975 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
976 | 1
977 |
978 |
979 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
980 | 1
981 |
982 |
983 |
984 |
985 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
986 | 1
987 |
988 |
989 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
990 | 1
991 |
992 |
993 |
994 |
995 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
996 | 1
997 |
998 |
999 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
1000 | 1
1001 |
1002 |
1003 |
1004 |
1005 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
1006 | 1
1007 |
1008 |
1009 | ..\$(PROJECTNAME).launchscreen\Assets\AppIcon.appiconset
1010 | 1
1011 |
1012 |
1013 |
1014 |
1015 | 1
1016 |
1017 |
1018 | 1
1019 |
1020 |
1021 |
1022 |
1023 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF
1024 | 1
1025 |
1026 |
1027 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF
1028 | 1
1029 |
1030 |
1031 |
1032 |
1033 | 1
1034 |
1035 |
1036 | 1
1037 |
1038 |
1039 |
1040 |
1041 | ..\
1042 | 1
1043 |
1044 |
1045 | ..\
1046 | 1
1047 |
1048 |
1049 |
1050 |
1051 | 1
1052 |
1053 |
1054 | 1
1055 |
1056 |
1057 | 1
1058 |
1059 |
1060 |
1061 |
1062 | ..\$(PROJECTNAME).launchscreen
1063 | 64
1064 |
1065 |
1066 | ..\$(PROJECTNAME).launchscreen
1067 | 64
1068 |
1069 |
1070 |
1071 |
1072 | 1
1073 |
1074 |
1075 | 1
1076 |
1077 |
1078 | 1
1079 |
1080 |
1081 |
1082 |
1083 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF
1084 | 1
1085 |
1086 |
1087 |
1088 |
1089 | ..\
1090 | 1
1091 |
1092 |
1093 | ..\
1094 | 1
1095 |
1096 |
1097 |
1098 |
1099 | Contents
1100 | 1
1101 |
1102 |
1103 | Contents
1104 | 1
1105 |
1106 |
1107 |
1108 |
1109 | Contents\Resources
1110 | 1
1111 |
1112 |
1113 | Contents\Resources
1114 | 1
1115 |
1116 |
1117 |
1118 |
1119 | library\lib\armeabi-v7a
1120 | 1
1121 |
1122 |
1123 | library\lib\arm64-v8a
1124 | 1
1125 |
1126 |
1127 | 1
1128 |
1129 |
1130 | 1
1131 |
1132 |
1133 | 1
1134 |
1135 |
1136 | 1
1137 |
1138 |
1139 | Contents\MacOS
1140 | 1
1141 |
1142 |
1143 | Contents\MacOS
1144 | 1
1145 |
1146 |
1147 | 0
1148 |
1149 |
1150 |
1151 |
1152 | library\lib\armeabi-v7a
1153 | 1
1154 |
1155 |
1156 |
1157 |
1158 | 1
1159 |
1160 |
1161 | 1
1162 |
1163 |
1164 |
1165 |
1166 | Assets
1167 | 1
1168 |
1169 |
1170 | Assets
1171 | 1
1172 |
1173 |
1174 |
1175 |
1176 | Assets
1177 | 1
1178 |
1179 |
1180 | Assets
1181 | 1
1182 |
1183 |
1184 |
1185 |
1186 |
1187 |
1188 |
1189 |
1190 |
1191 |
1192 |
1193 |
1194 |
1195 |
1196 | True
1197 | False
1198 |
1199 |
1200 | 12
1201 |
1202 |
1203 |
1204 |
1205 |
1206 |
--------------------------------------------------------------------------------
/sample/apiExample.res:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GabrielTrigo/telegram-bot-delphi/3c6be2092d03ce49fbd0bbe9b0802d1ee697a1fb/sample/apiExample.res
--------------------------------------------------------------------------------
/sample/uFrmMain.dfm:
--------------------------------------------------------------------------------
1 | object Form1: TForm1
2 | Left = 0
3 | Top = 0
4 | BorderIcons = [biSystemMenu, biMinimize]
5 | BorderStyle = bsSingle
6 | ClientHeight = 412
7 | ClientWidth = 411
8 | Color = clBtnFace
9 | Font.Charset = DEFAULT_CHARSET
10 | Font.Color = clWindowText
11 | Font.Height = -11
12 | Font.Name = 'Consolas'
13 | Font.Style = []
14 | OldCreateOrder = False
15 | Position = poScreenCenter
16 | OnCreate = FormCreate
17 | PixelsPerInch = 96
18 | TextHeight = 13
19 | object SpeedButton1: TSpeedButton
20 | Left = 178
21 | Top = 56
22 | Width = 87
23 | Height = 21
24 | Caption = 'Save'
25 | OnClick = SpeedButton1Click
26 | end
27 | object Label1: TLabel
28 | Left = 5
29 | Top = 43
30 | Width = 48
31 | Height = 13
32 | Caption = 'User Id:'
33 | end
34 | object Label2: TLabel
35 | Left = 5
36 | Top = 5
37 | Width = 60
38 | Height = 13
39 | Caption = 'Bot Token:'
40 | end
41 | object BtnSendFile: TButton
42 | Left = 106
43 | Top = 91
44 | Width = 95
45 | Height = 25
46 | Caption = 'SendFile'
47 | TabOrder = 0
48 | OnClick = BtnSendFileClick
49 | end
50 | object EdtTokenBot: TEdit
51 | Left = 5
52 | Top = 18
53 | Width = 260
54 | Height = 21
55 | Color = clSilver
56 | TabOrder = 1
57 | end
58 | object MemLog: TMemo
59 | Left = 0
60 | Top = 158
61 | Width = 411
62 | Height = 254
63 | Align = alBottom
64 | BorderStyle = bsNone
65 | Color = 14277119
66 | Font.Charset = DEFAULT_CHARSET
67 | Font.Color = clBlack
68 | Font.Height = -13
69 | Font.Name = 'Tahoma'
70 | Font.Style = []
71 | ParentFont = False
72 | ReadOnly = True
73 | ScrollBars = ssVertical
74 | TabOrder = 2
75 | ExplicitLeft = 5
76 | ExplicitTop = 153
77 | end
78 | object BtnSendMsg: TButton
79 | Left = 5
80 | Top = 91
81 | Width = 95
82 | Height = 25
83 | Caption = 'SendMsg'
84 | TabOrder = 3
85 | OnClick = BtnSendMsgClick
86 | end
87 | object BtnSendWithButtons: TButton
88 | Left = 5
89 | Top = 122
90 | Width = 196
91 | Height = 25
92 | Caption = 'SendMsgWithButtons'
93 | TabOrder = 4
94 | OnClick = BtnSendWithButtonsClick
95 | end
96 | object BtnGetUpdates: TButton
97 | Left = 207
98 | Top = 91
99 | Width = 95
100 | Height = 25
101 | Caption = 'GetUpdates'
102 | TabOrder = 5
103 | OnClick = BtnGetUpdatesClick
104 | end
105 | object BtnSendLocation: TButton
106 | Left = 308
107 | Top = 91
108 | Width = 95
109 | Height = 25
110 | Caption = 'SendLocation'
111 | TabOrder = 6
112 | OnClick = BtnSendLocationClick
113 | end
114 | object BtnStartMonitor: TButton
115 | Left = 207
116 | Top = 122
117 | Width = 196
118 | Height = 25
119 | Caption = 'Start monitor'
120 | TabOrder = 7
121 | OnClick = BtnStartMonitorClick
122 | end
123 | object EdtUserId: TEdit
124 | Left = 5
125 | Top = 56
126 | Width = 167
127 | Height = 21
128 | Color = clSilver
129 | TabOrder = 8
130 | end
131 | end
132 |
--------------------------------------------------------------------------------
/sample/uFrmMain.pas:
--------------------------------------------------------------------------------
1 | unit uFrmMain;
2 |
3 | interface
4 |
5 | uses
6 | System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls,
7 | Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Buttons, System.IOUtils,
8 | uTelegramAPI, uTelegramAPI.Interfaces, uConsts, uClassMessageDTO;
9 |
10 | type
11 | TForm1 = class(TForm)
12 | BtnSendFile: TButton;
13 | EdtTokenBot: TEdit;
14 | MemLog: TMemo;
15 | BtnSendMsg: TButton;
16 | BtnSendWithButtons: TButton;
17 | BtnGetUpdates: TButton;
18 | BtnSendLocation: TButton;
19 | BtnStartMonitor: TButton;
20 | SpeedButton1: TSpeedButton;
21 | EdtUserId: TEdit;
22 | Label1: TLabel;
23 | Label2: TLabel;
24 | procedure FormCreate(Sender: TObject);
25 | procedure BtnSendFileClick(Sender: TObject);
26 | procedure BtnSendMsgClick(Sender: TObject);
27 | procedure BtnSendWithButtonsClick(Sender: TObject);
28 | procedure BtnGetUpdatesClick(Sender: TObject);
29 | procedure BtnSendLocationClick(Sender: TObject);
30 | procedure BtnStartMonitorClick(Sender: TObject);
31 | procedure SpeedButton1Click(Sender: TObject);
32 | private
33 | { Private declarations }
34 | FTelegram: iTelegramAPI;
35 | FAsyncHttp: TThread;
36 | procedure OnAPIError(AExcept: Exception);
37 | public
38 | { Public declarations }
39 | end;
40 |
41 | var
42 | Form1: TForm1;
43 |
44 | implementation
45 |
46 | {$R *.dfm}
47 |
48 | procedure TForm1.FormCreate(Sender: TObject);
49 | begin
50 | EdtTokenBot.Text := 'YOUR_API_TOKEN_HERE';
51 |
52 | // use the button "getUpdates" for get chats IDs(only messages sent to your bot)
53 | EdtUserId.Text := 'USERID_FOR_SEND_MESSAGES';
54 |
55 | FTelegram := TTelegramAPI.New();
56 | FTelegram
57 | .OnError(OnAPIError)
58 | .SetUserID(EdtUserId.Text)
59 | .SetBotToken(EdtTokenBot.Text);
60 |
61 | MemLog.Clear;
62 | end;
63 |
64 | procedure TForm1.BtnSendFileClick(Sender: TObject);
65 | begin
66 | FTelegram.SendFile('C:\File.zip');
67 |
68 | MemLog.Text := FTelegram.GetResult();
69 | end;
70 |
71 | procedure TForm1.BtnSendMsgClick(Sender: TObject);
72 | begin
73 | FTelegram.SendMsg('Hey there!');
74 |
75 | MemLog.Text := FTelegram.GetResult();
76 | end;
77 |
78 | procedure TForm1.BtnSendWithButtonsClick(Sender: TObject);
79 | var
80 | lButtons: TTelegramButtons;
81 | begin
82 | lButtons := TTelegramButtons.Create;
83 |
84 | try
85 | with lButtons do
86 | begin
87 | Add('Lamp 1 Off', 'https://domain.com/lamp1/off');
88 | Add('Lamp 1 On', 'https://domain.com/lamp1/on');
89 | Add('Lamp 2 Off', 'https://domain.com/lamp2/off');
90 | Add('Lamp 2 On', 'https://domain.com/lamp2/on');
91 | end;
92 |
93 | FTelegram.SendMsgWithButtons('Hi!', lButtons);
94 | finally
95 | FreeAndNil(lButtons);
96 | end;
97 | end;
98 |
99 | procedure TForm1.BtnGetUpdatesClick(Sender: TObject);
100 | var
101 | pChatList: TChatMessageDTOList;
102 | pChat: TChatMessageDTO;
103 | begin
104 | pChatList := TChatMessageDTOList.Create;
105 | pChat := TChatMessageDTO.Create;
106 |
107 | try
108 | FTelegram.GetUpdates(pChatList);
109 |
110 | for pChat in pChatList do
111 | ShowMessage(pChat.Message.Text + ' - ' + pChat.Message.From.Id.ToString);
112 | finally
113 | FreeAndNil(pChatList);
114 | end;
115 | end;
116 |
117 | procedure TForm1.BtnStartMonitorClick(Sender: TObject);
118 | begin
119 | if Assigned(FAsyncHttp) then Exit;
120 |
121 | FAsyncHttp := TThread.CreateAnonymousThread(
122 | procedure
123 | var
124 | lChatList: TChatMessageDTOList;
125 | begin
126 | lChatList := TChatMessageDTOList.Create;
127 |
128 | while True do
129 | begin
130 | lChatList.Clear;
131 |
132 | FTelegram.GetUpdates(lChatList);
133 |
134 | FAsyncHttp.Synchronize(FAsyncHttp,
135 | procedure
136 | var
137 | lChat: TChatMessageDTO;
138 | begin
139 | MemLog.Lines.Clear;
140 | MemLog.Lines.Add('-' + TimeToStr(Now()));
141 |
142 | for lChat in lChatList do
143 | MemLog.Lines.Add(lChat.Message.Text);
144 |
145 | MemLog.Lines.Add('-');
146 | end);
147 |
148 | FAsyncHttp.Sleep(5000);
149 | end;
150 |
151 | end);
152 |
153 | FAsyncHttp.Start();
154 | end;
155 |
156 | procedure TForm1.BtnSendLocationClick(Sender: TObject);
157 | begin
158 | FTelegram.SendLocation('51.519138', '-0.129028');
159 | end;
160 |
161 | procedure TForm1.OnAPIError(AExcept: Exception);
162 | begin
163 | TThread.Synchronize(TThread.Current, procedure
164 | begin
165 | MessageDlg(AExcept.Message, mtWarning, [mbOK], 0);
166 | end);
167 | end;
168 |
169 | procedure TForm1.SpeedButton1Click(Sender: TObject);
170 | begin
171 | FTelegram.SetBotToken(EdtTokenBot.Text);
172 | FTelegram.SetUserID(EdtUserId.Text);
173 | end;
174 |
175 | end.
176 |
--------------------------------------------------------------------------------
/src/uClassMessageDTO.pas:
--------------------------------------------------------------------------------
1 | unit uClassMessageDTO;
2 |
3 | interface
4 |
5 | uses
6 | REST.Json.Types;
7 |
8 | {$M+}
9 |
10 | type
11 | TChatDTO = class
12 | private
13 | FFirst_Name: string;
14 | FId: Integer;
15 | FLast_Name: string;
16 | FType: string;
17 | FUsername: string;
18 | published
19 | property First_Name: string read FFirst_Name write FFirst_Name;
20 | property Id: Integer read FId write FId;
21 | property Last_Name: string read FLast_Name write FLast_Name;
22 | property &Type: string read FType write FType;
23 | property Username: string read FUsername write FUsername;
24 | end;
25 |
26 | TFromDTO = class
27 | private
28 | FFirst_Name: string;
29 | FId: Integer;
30 | FIs_Bot: Boolean;
31 | FLanguage_Code: string;
32 | FLast_Name: string;
33 | FUsername: string;
34 | published
35 | property First_Name: string read FFirst_Name write FFirst_Name;
36 | property Id: Integer read FId write FId;
37 | property Is_Bot: Boolean read FIs_Bot write FIs_Bot;
38 | property Language_Code: string read FLanguage_Code write FLanguage_Code;
39 | property Last_Name: string read FLast_Name write FLast_Name;
40 | property Username: string read FUsername write FUsername;
41 | end;
42 |
43 | TMessageDTO = class
44 | private
45 | FChat: TChatDTO;
46 | FDate: Integer;
47 | FFrom: TFromDTO;
48 | FMessage_Id: Integer;
49 | FText: string;
50 | published
51 | property Chat: TChatDTO read FChat write FChat;
52 | property Date: Integer read FDate write FDate;
53 | property From: TFromDTO read FFrom write FFrom;
54 | property Message_Id: Integer read FMessage_Id write FMessage_Id;
55 | property Text: string read FText write FText;
56 | public
57 | constructor Create;
58 | destructor Destroy; override;
59 | end;
60 |
61 | TChatMessageDTO = class
62 | private
63 | [JSONNameAttribute('message')]
64 | FMessage: TMessageDTO;
65 | FUpdate_Id: Integer;
66 | published
67 | property Message: TMessageDTO read FMessage write FMessage;
68 | property Update_Id: Integer read FUpdate_Id write FUpdate_Id;
69 | public
70 | constructor Create;
71 | destructor Destroy; override;
72 | end;
73 |
74 | implementation
75 |
76 | { TMessageDTO }
77 |
78 | constructor TMessageDTO.Create;
79 | begin
80 | inherited;
81 | FFrom := TFromDTO.Create;
82 | FChat := TChatDTO.Create;
83 | end;
84 |
85 | destructor TMessageDTO.Destroy;
86 | begin
87 | FFrom.Free;
88 | FChat.Free;
89 | inherited;
90 | end;
91 |
92 | { TChatMessageDTO }
93 |
94 | constructor TChatMessageDTO.Create;
95 | begin
96 | FMessage := TMessageDTO.Create;
97 | end;
98 |
99 | destructor TChatMessageDTO.Destroy;
100 | begin
101 | FMessage.Free;
102 | inherited;
103 | end;
104 |
105 | end.
106 |
--------------------------------------------------------------------------------
/src/uConsts.pas:
--------------------------------------------------------------------------------
1 | unit uConsts;
2 |
3 | interface
4 |
5 | uses
6 | System.Generics.Collections, System.SysUtils, uClassMessageDTO;
7 |
8 | type
9 | TTelegramButtons = TDictionary;
10 | TTelegramButton = TPair;
11 | TProcErrorException = procedure(AExcept: Exception) of object;
12 | TChatMessageDTOList = TObjectList;
13 |
14 | implementation
15 |
16 | end.
17 |
--------------------------------------------------------------------------------
/src/uTelegramAPI.Interfaces.pas:
--------------------------------------------------------------------------------
1 | unit uTelegramAPI.Interfaces;
2 |
3 | interface
4 |
5 | uses uConsts, System.SysUtils, uClassMessageDTO;
6 |
7 | type
8 | iTelegramAPI = interface
9 | ['{11C947AD-6E74-47BF-8056-1EAA96D3A925}']
10 | function SetBotToken(AToken: String): iTelegramAPI;
11 | function SetUserID(AUserID: String): iTelegramAPI;
12 | function SendFile(AFileName: String): iTelegramAPI;
13 | function SendMsg(AMsg: String): iTelegramAPI;
14 | function SendMsgWithButtons(AMsg: String; AButtons: TTelegramButtons)
15 | : iTelegramAPI;
16 | function OnError(AValue: TProcErrorException): iTelegramAPI;
17 | function GetUpdates(var AValue: TChatMessageDTOList): iTelegramAPI;
18 | function GetResult(): String;
19 | function SendLocation(ALatitude, ALongitude: String): iTelegramAPI;
20 | end;
21 |
22 | implementation
23 |
24 | end.
25 |
--------------------------------------------------------------------------------
/src/uTelegramAPI.pas:
--------------------------------------------------------------------------------
1 | unit uTelegramAPI;
2 |
3 | interface
4 |
5 | uses
6 | uTelegramAPI.Interfaces, uConsts, System.Net.HttpClientComponent,
7 | System.SysUtils, System.Classes, System.Net.Mime, System.JSON,
8 | uClassMessageDTO, Rest.JSON, Rest.JSON.Types;
9 |
10 | // UnixToDateTime(1605808194) DateUtils
11 |
12 | type
13 | TTelegramAPI = Class(TInterfacedObject, iTelegramAPI)
14 | private
15 | FHTTPClient: TNetHTTPClient;
16 | FBotToken: String;
17 | FUserID: String;
18 | FResult: String;
19 | FProcErrorException: TProcErrorException;
20 | function _POST(AUrl: String; AData: TStrings): String; overload;
21 | function _POST(AUrl: String; AData: TMultipartFormData): String; overload;
22 | function _GET(AUrl: String): String;
23 | function GetURL(APath: String = ''): String;
24 | function Ready(): Boolean;
25 | public
26 | constructor Create();
27 | destructor Destroy(); override;
28 | class function New(): iTelegramAPI;
29 | function SetBotToken(AToken: String): iTelegramAPI;
30 | function SetUserID(AUserID: String): iTelegramAPI;
31 | function SendFile(AFileName: String): iTelegramAPI;
32 | function SendMsg(AMsg: String): iTelegramAPI;
33 | function SendMsgWithButtons(AMsg: String; AButtons: TTelegramButtons)
34 | : iTelegramAPI;
35 | function OnError(AValue: TProcErrorException): iTelegramAPI;
36 | function GetResult: String;
37 | function GetUpdates(var AValue: TChatMessageDTOList): iTelegramAPI;
38 | function SendLocation(ALatitude, ALongitude: String): iTelegramAPI;
39 | end;
40 |
41 | const
42 | urlBase = 'https://api.telegram.org/{BOT_TOKEN}';
43 |
44 | implementation
45 |
46 | { TTelegramAPI }
47 |
48 | constructor TTelegramAPI.Create;
49 | begin
50 | FHTTPClient := TNetHTTPClient.Create(nil);
51 | FHTTPClient.ConnectionTimeout := 5000;
52 | FHTTPClient.ResponseTimeout := 5000;
53 | end;
54 |
55 | destructor TTelegramAPI.Destroy;
56 | begin
57 | FreeAndNil(FHTTPClient);
58 | inherited;
59 | end;
60 |
61 | class function TTelegramAPI.New: iTelegramAPI;
62 | begin
63 | Result := Self.Create;
64 | end;
65 |
66 | function TTelegramAPI.OnError(AValue: TProcErrorException): iTelegramAPI;
67 | begin
68 | Result := Self;
69 | FProcErrorException := AValue;
70 | end;
71 |
72 | function TTelegramAPI.Ready: Boolean;
73 | begin
74 | Result := True;
75 |
76 | if FBotToken.IsEmpty then
77 | begin
78 | if Assigned(FProcErrorException) then
79 | FProcErrorException(Exception.Create('BotToken is Empty!'));
80 | Result := False;
81 | end
82 | else if FUserID.IsEmpty then
83 | begin
84 | if Assigned(FProcErrorException) then
85 | FProcErrorException(Exception.Create('UserID is Empty!'));
86 | Result := False;
87 | end;
88 | end;
89 |
90 | function TTelegramAPI.GetResult: String;
91 | begin
92 | Result := FResult;
93 | end;
94 |
95 | function TTelegramAPI.GetUpdates(var AValue: TChatMessageDTOList): iTelegramAPI;
96 | var
97 | lArrJSON: TJSONArray;
98 | I: Byte;
99 | begin
100 | Result := Self;
101 |
102 | if FBotToken.IsEmpty then
103 | begin
104 | if Assigned(FProcErrorException) then
105 | FProcErrorException(Exception.Create('BotToken is Empty!'));
106 | Exit;
107 | end;
108 |
109 | try
110 | FResult := _GET(GetURL('/getUpdates'));
111 |
112 | lArrJSON := ((TJSONObject.ParseJSONValue(FResult) as TJSONObject)
113 | .GetValue('result') as TJSONArray);
114 |
115 | if lArrJSON.Count <= 0 then Exit;
116 |
117 | for I := 0 to Pred(lArrJSON.Count) do
118 | AValue.Add(TJSON.JsonToObject(lArrJSON.Items[I].ToJSON));
119 |
120 | lArrJSON.Destroy;
121 | except
122 | on E: Exception do
123 | begin
124 | if Assigned(FProcErrorException) then
125 | FProcErrorException(E);
126 | end;
127 | end;
128 | end;
129 |
130 | function TTelegramAPI.GetURL(APath: String = ''): String;
131 | begin
132 | Result := EmptyStr;
133 |
134 | try
135 | Result := urlBase.Replace('{BOT_TOKEN}', FBotToken + APath);
136 | except
137 | end;
138 | end;
139 |
140 | function TTelegramAPI.SendFile(AFileName: String): iTelegramAPI;
141 | var
142 | pData: TMultipartFormData;
143 | begin
144 | Result := Self;
145 | FHTTPClient.ContentType := 'multipart/form-data';
146 |
147 | pData := TMultipartFormData.Create;
148 | pData.AddFile('document', AFileName);
149 | pData.AddField('chat_id', FUserID);
150 |
151 | FResult := _POST(GetURL('/sendDocument'), pData);
152 | end;
153 |
154 | function TTelegramAPI.SendLocation(ALatitude, ALongitude: String): iTelegramAPI;
155 | var
156 | pData: TStrings;
157 | begin
158 | Result := Self;
159 |
160 | FHTTPClient.ContentType := 'application/json';
161 |
162 | pData := TStringList.Create;
163 | pData.AddPair('chat_id', FUserID);
164 | pData.AddPair('latitude', ALatitude);
165 | pData.AddPair('longitude', ALongitude);
166 |
167 | FResult := _POST(GetURL('/sendLocation'), pData);
168 | end;
169 |
170 | function TTelegramAPI.SendMsg(AMsg: String): iTelegramAPI;
171 | var
172 | pData: TStrings;
173 | begin
174 | Result := Self;
175 |
176 | FHTTPClient.ContentType := 'application/json';
177 |
178 | pData := TStringList.Create;
179 | pData.AddPair('chat_id', FUserID);
180 | pData.AddPair('text', AMsg);
181 |
182 | FResult := _POST(GetURL('/sendMessage'), pData);
183 | end;
184 |
185 | function TTelegramAPI.SendMsgWithButtons(AMsg: String;
186 | AButtons: TTelegramButtons): iTelegramAPI;
187 | var
188 | lData: TStrings;
189 | lJsonArr: TJSONArray;
190 | lButton: TTelegramButton;
191 | begin
192 | Result := Self;
193 |
194 | if AButtons.Count <= 0 then
195 | Exit;
196 |
197 | lJsonArr := TJSONArray.Create;
198 |
199 | for lButton in AButtons do
200 | begin
201 | lJsonArr.AddElement(TJSONObject.Create.AddPair('text', lButton.Key)
202 | .AddPair('url', lButton.Value));
203 | end;
204 |
205 | FHTTPClient.ContentType := 'application/json';
206 |
207 | lData := TStringList.Create;
208 | lData.AddPair('chat_id', FUserID);
209 | lData.AddPair('text', AMsg);
210 | lData.AddPair('reply_markup', '{"inline_keyboard":[' +
211 | lJsonArr.ToJSON + ']}');
212 |
213 | FResult := _POST(GetURL('/sendMessage'), lData);
214 | end;
215 |
216 | function TTelegramAPI.SetBotToken(AToken: String): iTelegramAPI;
217 | begin
218 | Result := Self;
219 | FBotToken := 'bot' + AToken;
220 | end;
221 |
222 | function TTelegramAPI.SetUserID(AUserID: String): iTelegramAPI;
223 | begin
224 | Result := Self;
225 | FUserID := AUserID;
226 | end;
227 |
228 | function TTelegramAPI._GET(AUrl: String): String;
229 | begin
230 | Result := EmptyStr;
231 |
232 | try
233 | Result := FHTTPClient.Get(AUrl).ContentAsString(TEncoding.UTF8);
234 | except
235 | on E: Exception do
236 | begin
237 | if Assigned(FProcErrorException) then
238 | FProcErrorException(E);
239 | end;
240 | end;
241 | end;
242 |
243 | function TTelegramAPI._POST(AUrl: String; AData: TMultipartFormData): String;
244 | begin
245 | Result := EmptyStr;
246 |
247 | if not Ready() then
248 | Exit;
249 |
250 | try
251 | Result := FHTTPClient.Post(AUrl, AData).ContentAsString(TEncoding.UTF8);
252 | except
253 | on E: Exception do
254 | begin
255 | if Assigned(FProcErrorException) then
256 | FProcErrorException(E);
257 | end;
258 | end;
259 | end;
260 |
261 | function TTelegramAPI._POST(AUrl: String; AData: TStrings): String;
262 | begin
263 | Result := EmptyStr;
264 |
265 | if not Ready() then
266 | Exit;
267 |
268 | try
269 | Result := FHTTPClient.Post(AUrl, AData).ContentAsString(TEncoding.UTF8);
270 | except
271 | on E: Exception do
272 | begin
273 | if Assigned(FProcErrorException) then
274 | FProcErrorException(E);
275 | end;
276 | end;
277 | end;
278 |
279 | end.
280 |
--------------------------------------------------------------------------------