-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg-bot-example.ps1
119 lines (95 loc) · 2.91 KB
/
tg-bot-example.ps1
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
$token = "1491600444:AAFn7fuaFUrJZahN-lx6kkpCfmP21OMsSK4"
$baseUrl = "https://api.telegram.org/bot$token/"
$updatesOffsetStorageFileName = "$PSScriptRoot\offset_storage";
function log($msg) {
Write-Information -MessageData $msg -InformationAction Continue;
}
function isExistOffsetStorage() {
return Test-Path -path $updatesOffsetStorageFileName;
}
function createOffsetStorage() {
$status = isExistOffsetStorage;
if ($status -eq $False) {
log("create offset storage");
New-Item $updatesOffsetStorageFileName;
}
}
function writeToOffsetStorage($text) {
$text | Set-Content $updatesOffsetStorageFileName
}
function getOffsetFromStorage() {
if (isExistOffsetStorage) {
return Get-Content -Path $updatesOffsetStorageFileName -TotalCount 1
}
else {
throw 'storage not found';
}
}
function setSleepInterval($time = 5) {
log("Start sleep; time: $time");
Start-Sleep -s $time;
}
function request($path, $body) {
log("request: $path");
$response = Invoke-WebRequest -UseBasicParsing -Uri ($baseUrl + $path) -Method POST -Body $body;
log("response: $response");
return $response | ConvertFrom-Json;
}
function validateResponse($response) {
if (!$response.ok) {
throw 'bad respose';
}
}
function getUpdates($timeout = 15) {
$status = isExistOffsetStorage;
if ($status -eq $true) {
$offsetFromStorage = getOffsetFromStorage;
$offset = [int]$offsetFromStorage + 1;
log("Send getUpdates. Wait events. timeout=$timeout; offset=$offset");
return request("getUpdates?timeout=$timeout&offset=$offset");
}
log("Send getUpdates. Wait events. timeout=$timeout");
return request("getUpdates?timeout=$timeout");
}
function sendMessage($msg) {
#need validation requeries params
$postParams = @{
chat_id = $msg.chat.id;
text = $msg.text
}
Invoke-WebRequest -UseBasicParsing -Uri ($baseUrl + "sendMessage") -Method POST -Body $postParams;
}
function processMessages($resultArr) {
if ($resultArr.count -lt 1) {
return;
}
log("start process messages");
foreach ($msg in $resultArr) {
$msgObj = $msg.message;
if ($msgObj) {
sendMessage($msgObj);
}
}
log("end process messages");
}
function saveLastUpdateId($result) {
if (isExistOffsetStorage -eq $false) {
createOffsetStorage;
}
writeToOffsetStorage($result[$result.count - 1].update_id);
}
while ($true) {
#send long pool request
$response = getUpdates;
if (!$response.ok) {
log("getUpdates failure. "); setSleepInterval; continue;
}
elseif (!$response.result.count) {
log("getUpdates success. response.result is empty"); setSleepInterval; continue;
}
#start messages processor
processMessages($response.result);
#save last update id
saveLastUpdateId($response.result);
setSleepInterval;
}