Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
hrbot
Manage
Activity
Members
Labels
Plan
Issues
9
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Aleksandr Kirienko
hrbot
Merge requests
!3
Bot make company
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Bot make company
bot-make-company
into
master
Overview
1
Commits
10
Pipelines
0
Changes
6
Merged
Aleksandr Kirienko
requested to merge
bot-make-company
into
master
4 years ago
Overview
1
Commits
10
Pipelines
0
Changes
6
Expand
Created by: ZzDmitry
0
0
Merge request reports
Compare
master
master (base)
and
latest version
latest version
2be97aff
10 commits,
2 years ago
6 files
+
211
−
35
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
6
Search (e.g. *.vue) (Ctrl+P)
bot/index.js
+
115
−
2
Options
@@ -2,6 +2,7 @@ const TelegramBot = require('node-telegram-bot-api');
const
express
=
require
(
'
express
'
);
const
bodyParser
=
require
(
'
body-parser
'
);
const
config
=
require
(
'
hrbot-common
'
).
config
;
const
{
services
}
=
require
(
'
../common
'
);
async
function
botSetWebHook
(
bot
,
botUrl
)
{
@@ -52,9 +53,121 @@ async function main() {
await
startBotServer
(
bot
,
botPath
,
config
.
serverPort
);
console
.
log
(
'
Bot started.
'
);
bot
.
on
(
'
message
'
,
(
msg
)
=>
{
bot
.
on
(
'
message
'
,
async
(
msg
)
=>
{
console
.
log
(
'
message
'
,
msg
);
bot
.
sendMessage
(
msg
.
from
.
id
,
`>>>
${
msg
.
text
}
`
);
const
text
=
msg
.
text
;
if
(
!
text
)
{
return
;
}
let
m
;
if
(
m
=
text
.
match
(
/^
\/
add-company
\s?(
.*
)?
/
))
{
const
args
=
m
[
1
];
if
(
!
args
)
{
bot
.
sendMessage
(
msg
.
from
.
id
,
`Подсказка: /add-company Название*адрес*имя бота*токен бота`
);
return
;
}
const
[
name
,
address
,
botName
,
botToken
]
=
args
.
split
(
'
*
'
).
map
(
s
=>
s
.
trim
());
if
(
!
name
||
!
address
||
!
botName
||
!
botToken
)
{
bot
.
sendMessage
(
msg
.
from
.
id
,
`Подсказка, введено не всё: /add-company Название*адрес*имя бота*токен бота`
);
return
;
}
try
{
const
result
=
await
services
.
companiesService
.
add
(
name
,
botToken
,
botName
,
address
);
console
.
log
(
'
Company added
'
,
result
);
bot
.
sendMessage
(
msg
.
from
.
id
,
'
Организация добавлена
'
);
}
catch
(
err
)
{
console
.
error
(
'
Company adding error
'
,
err
);
bot
.
sendMessage
(
msg
.
from
.
id
,
'
Ошибка добавления организации
'
);
}
return
;
}
if
(
m
=
text
.
match
(
/^
\/
add-user
\s?(
.*
)?
/
))
{
const
args
=
m
[
1
];
if
(
!
args
)
{
bot
.
sendMessage
(
msg
.
from
.
id
,
`Подсказка: /add-user телефон`
);
return
;
}
const
phone
=
args
.
replace
(
/
\D
/g
,
''
);
const
companies
=
await
services
.
companiesService
.
getAll
();
console
.
log
(
companies
);
if
(
!
companies
.
length
)
{
bot
.
sendMessage
(
msg
.
from
.
id
,
'
Нет компаний для добавления пользователя
'
,
);
return
;
}
bot
.
sendMessage
(
msg
.
from
.
id
,
`Добавить пользователя с телефоном
${
phone
}
в компанию`
,
{
reply_markup
:
JSON
.
stringify
({
inline_keyboard
:
companies
.
map
(
company
=>
([{
text
:
`
${
company
.
id
}
${
company
.
botName
}
`
,
callback_data
:
JSON
.
stringify
([
'
u
'
,
'
c
'
,
`
${
phone
}
\n
${
company
.
id
}
`
,
]),
}])),
}),
},
);
return
;
}
if
(
m
=
text
.
match
(
/^
\/
update-user
\s?(
.*
)?
/
))
{
const
args
=
m
[
1
];
if
(
!
args
)
{
bot
.
sendMessage
(
msg
.
from
.
id
,
'
Подсказка: /update-user id*[телефон][*[telegramId]]
'
);
return
;
}
const
[
userId
,
phone
,
telegramId
]
=
args
.
split
(
'
*
'
).
map
(
s
=>
s
.
trim
());
if
(
!
userId
)
{
bot
.
sendMessage
(
msg
.
from
.
id
,
'
Подсказка, не введён пользователь: /update-user id*[телефон][*[telegramId]]
'
);
return
;
}
if
(
!
phone
&&
!
telegramId
)
{
bot
.
sendMessage
(
msg
.
from
.
id
,
'
Не введено изменений для пользователя
'
);
return
;
}
/*
const userToUpdate = await services.usersService.getUserById(userId);
console.log('User to update', userToUpdate);
if (!userToUpdate) {
bot.sendMessage(msg.from.id, 'Нет введённого пользователя');
return;
}
*/
try
{
const
updatedUser
=
await
services
.
usersService
.
update
(
userId
,
{
phone
,
telegramId
});
console
.
error
(
'
User updated
'
,
updatedUser
);
bot
.
sendMessage
(
msg
.
from
.
id
,
'
Пользователь изменён
'
);
}
catch
(
error
)
{
console
.
error
(
'
User updating error
'
,
error
);
bot
.
sendMessage
(
msg
.
from
.
id
,
'
Ошибка изменения пользователя
'
);
}
return
;
}
bot
.
sendMessage
(
msg
.
from
.
id
,
`Непонятно:
${
msg
.
text
}
`
);
});
bot
.
on
(
'
callback_query
'
,
async
(
msg
)
=>
{
console
.
log
(
'
CQ
'
,
msg
);
const
[
type
,
action
,
data
]
=
JSON
.
parse
(
msg
.
data
);
if
(
type
===
'
u
'
&&
action
===
'
c
'
)
{
try
{
const
[
phone
,
companyId
]
=
data
.
split
(
'
\n
'
);
console
.
log
(
'
Creating user...
'
,
phone
,
companyId
);
const
user
=
await
services
.
usersService
.
add
(
phone
,
+
companyId
);
console
.
log
(
'
User created
'
,
user
);
bot
.
sendMessage
(
msg
.
from
.
id
,
'
Пользователь создан
'
);
}
catch
(
error
)
{
console
.
error
(
'
User creation error
'
,
error
);
bot
.
sendMessage
(
msg
.
from
.
id
,
'
Ошибка создания пользователя
'
);
}
bot
.
answerCallbackQuery
(
msg
.
id
)
return
;
}
});
}
Loading