Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
Inventory tg bot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
globa
Inventory tg bot
Commits
02c247b9
Commit
02c247b9
authored
May 17, 2024
by
globa
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parents
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
101 additions
and
0 deletions
+101
-0
.gitignore
.gitignore
+3
-0
db.sqlite1
db.sqlite1
+0
-0
test.ps1
test.ps1
+98
-0
No files found.
.gitignore
0 → 100644
View file @
02c247b9
*.cfg
*.sqlite
\ No newline at end of file
db.sqlite1
0 → 100644
View file @
02c247b9
File added
test.ps1
0 → 100644
View file @
02c247b9
$db
=
"D:\telegram-bot\db.sqlite"
$ids
=
(
'177113518'
,
'162513986'
)
#Invoke-SqliteQuery -DataBase $db -Query $query
# Token
$token
=
"320805042:AAFsnz44hhAmHvUy4i3fgT9oAA9xk0VbkHc"
# Telegram URLs
$URL_get
=
"https://api.telegram.org/bot
$token
/getUpdates"
$URL_set
=
"https://api.telegram.org/bot
$token
/sendMessage"
# timeout sec
$timeout
=
1
function
getUpdates
(
$URL
)
{
$json
=
Invoke-RestMethod
-Uri
$URL
$data
=
$json
.result |
Select-Object
-Last 1
#$data.update_id
$chat_id
=
$data
.message.chat.id
$text
=
$data
.message.text
$f_name
=
$data
.message.chat.first_name
$l_name
=
$data
.message.chat.last_name
$type
=
$data
.message.chat.type
$username
=
$data
.message.chat.username
# проверяем что text есть
if
(
$text
)
{
# confirm
Invoke-RestMethod
"
$(
$URL
)
?offset=
$($(
$data
.update_id
)
+1
)
"
-Method Get | Out-Null
# HashTable
$ht
=
@
{}
$ht
[
"chat_id"
]
=
$chat_id
$ht
[
"text"
]
=
$text
$ht
[
"f_name"
]
=
$f_name
$ht
[
"l_name"
]
=
$l_name
$ht
[
"username"
]
=
$username
return
$ht
}
}
function
sendMessage
(
$URL
,
$chat_id
,
$text
)
{
# создаем HashTable, можно объявлять ее и таким способом
$ht
=
@
{
text
=
$text
# указан способ разметки Markdown
parse_mode
=
"html"
chat_id
=
$chat_id
}
# Данные нужно отправлять в формате json
$json
=
$ht
|
ConvertTo-Json
# Делаем через Invoke-RestMethod, но никто не запрещает сделать и через Invoke-WebRequest
# Method Post - т.к. отправляем данные, по умолчанию Get
Invoke-RestMethod
$URL
-Method Post -ContentType
'application/json; charset=utf-8'
-Body
$json
| Out-Null
}
function
Get-YearEnding
([
int
]
$num
)
{
if
(
$num
-eq 1
)
{
return
"год"
}
elseif
(
$num
-in
(
2, 3, 4
))
{
return
"года"
}
else
{
return
"лет"
}
}
while
(
$true
)
# вечный цикл
{
$return
=
getUpdates
$URL_get
if
(
$return
)
{
if
(
$return
.chat_id -in
$ids
){
$response
=
Invoke-SqliteQuery -DataBase
$db
-Query
"SELECT * From inventory WHERE inventory_num LIKE '
$(
$return
[
"text"
]
.ToUpper
(
)
)'"
if
(
$response
-ne
$null
){
foreach
(
$r
in
$response
){
$TimeSpan
=
[
math]::Round
(((
Get-Date
)
-
[
DateTime]::ParseExact
(
$r
.date,
'dd.MM.yyyy'
,
$null
))
.Days / 365, 0
)
$text
=
"<b>
$(
$r
.inventory_num
)
</b>
Наименование: <b>
$(
$r
.name
)
</b>
МОЛ: <b>
$(
$r
.MOL
)
</b>
Подразделение: <b>
$(
$r
.department
)
</b>
Дата принятия к учету: <b>
$(
$r
.date
)
(
$(
$TimeSpan
)
$(
Get-YearEnding
(
$TimeSpan
)
) )</b>
Цена / Остаточная стоимость: <b>
$(
$r
.price
)
₽ /
$(
$r
.residual_price
)
₽</b>"
Write-Host
$text
sendMessage
$URL_set
$return
.chat_id
$text
}
}
else
{
sendMessage
$URL_set
$return
.chat_id
'Ничего не найдено'
}
}
else
{
sendMessage
$URL_set
$return
.chat_id
'У вас нет доступа к этому боту'
}
}
Start-Sleep
-s
$timeout
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment