-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtgbp.lpr
183 lines (141 loc) · 3.37 KB
/
tgbp.lpr
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
program tgbp;
{$mode objfpc}{$H+}
uses {$IFDEF UNIX} {$IFDEF UseCThreads}
cthreads, {$ENDIF} {$ENDIF}
Classes,
SysUtils,
CustApp,
fpjson,
jsonparser,
inifiles,
dateutils,
Process,
// internal classes
uKeyboardCls, uOtherFunctions, uFileCls;
type
Msg = record
chat_id: cardinal;
date: cardinal;
Text: string;
msg_id: int64;
end;
Steping = record
chat_id: cardinal;
step: cardinal;
end;
TMsg = array of Msg;
// const lis
{ TTgBotSKD }
TTgBotSKD = class(TCustomApplication)
protected
procedure DoRun; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;
private
function HttpGetBinaryEx(url: string; var mem: TMemoryStream): boolean;
function BotInit(): boolean;
end;
{ TTgBotSKD }
const
REC_START = '/start';
// gobal variables
var
cback: TStringStream;
ini: tinifile;
StepStore: array of Steping;
BOT_TOKKEN,URL_REQ: String;
mstr: tmemorystream;
JsonDoc: TJSONObject;
JsonParsera: TJSONParser;
procedure TTgBotSKD.DoRun;
var
ErrorMsg: string;
begin
// quick check parameters
ErrorMsg := CheckOptions('h', 'help');
if ErrorMsg <> '' then
begin
ShowException(Exception.Create(ErrorMsg));
Terminate;
Exit;
end;
// parse parameters
if HasOption('h', 'help') then
begin
WriteHelp;
Terminate;
Exit;
end;
{ add your program here }
BotInit();
// stop program loop
Terminate;
end;
constructor TTgBotSKD.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
ini := TIniFile.Create(GetAppDir()+'/config.ini');
{ // init ini file
ini.WriteString('APP','TOKKEN','123xxxxxxxx');
ini.WriteString('APP','LAST_MESSAGE','0');
// end init ini file }
BOT_TOKKEN:=ini.ReadString('APP','TOKKEN','');
URL_REQ := 'https://api.telegram.org/bot' + BOT_TOKKEN + '/';
end;
destructor TTgBotSKD.Destroy;
begin
inherited Destroy;
end;
procedure TTgBotSKD.WriteHelp;
begin
{ add your help code here }
writeln('Usage: ', ExeName, ' -h');
end;
function TTgBotSKD.HttpGetBinaryEx(url: string; var mem: TMemoryStream): boolean;
var
r: string;
begin
//WriteLn('/usr/bin/curl',['-o',ExtractFileDir(ExeName)+'/catch',url]);
if RunCommand('/usr/bin/curl', ['-o', ExtractFileDir(ExeName) +
'/catch', url, '-H',
'Content-Type: application/x-www-form-urlencoded; charset: UTF-8'],
r) then
begin
mem.LoadFromFile(ExtractFileDir(ExeName) + '/catch');
end;
Result := True;
end;
function TTgBotSKD.BotInit(): boolean;
begin
mstr := TMemoryStream.Create;
try
if HttpGetBinaryEx(URL_REQ + 'getMe', mstr) then
begin
mstr.position := 0;
JsonParsera := TJSONParser.Create(mstr);
JsonDoc := TJSONObject(JsonParsera.Parse);
if jsonDoc.findpath('ok').AsBoolean then
begin
WriteLn('@' + jsonDoc.findpath('result.username').AsString);
end;
end
else
begin
WriteLn('cant init bot...');
halt(0);
end;
finally
//HTTPClient.Free
end;
Result := True;
end;
var
Application: TTgBotSKD;
begin
Application := TTgBotSKD.Create(nil);
Application.Title := 'Telegram Bot SDK';
Application.Run;
Application.Free;
end.