mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-14 12:25:17 +00:00
Add tg contact if it is required (#436)
Signed-off-by: Ilya Sumbatyants <ilya.sumb@gmail.com>
This commit is contained in:
parent
2f70cdb5ff
commit
047815fc04
@ -61,20 +61,62 @@
|
||||
})
|
||||
const client = getClient()
|
||||
|
||||
async function onMessage(event: CustomEvent) {
|
||||
await fetch(url + '/send-msg', {
|
||||
async function sendMsg (to: string, msg: string) {
|
||||
return await fetch(url + '/send-msg', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: 'Bearer ' + getMetadata(login.metadata.LoginToken),
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
to: contactString?.value ?? '',
|
||||
msg: event.detail
|
||||
to,
|
||||
msg
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function addContact (phone: string) {
|
||||
const [lastName, firstName] = object.name.split(',')
|
||||
|
||||
return await fetch(url + '/add-contact', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: 'Bearer ' + getMetadata(login.metadata.LoginToken),
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
firstName: firstName ?? '',
|
||||
lastName: lastName ?? '',
|
||||
phone
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function onMessage(event: CustomEvent) {
|
||||
const to = contactString?.value ?? ''
|
||||
const sendRes = await sendMsg(to, event.detail)
|
||||
|
||||
if (sendRes.status !== 400 || !to.startsWith('+')) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
const err = await sendRes.json()
|
||||
if (err.code !== 'CONTACT_IMPORT_REQUIRED') {
|
||||
return
|
||||
}
|
||||
|
||||
const addRes = await addContact(to)
|
||||
|
||||
if (Math.trunc(addRes.status / 100) !== 2) {
|
||||
const { message } = await addRes.json().catch(() => ({ message: 'Unknown error' }))
|
||||
|
||||
throw Error(message)
|
||||
}
|
||||
|
||||
await sendMsg(to, event.detail)
|
||||
}
|
||||
|
||||
function isNewDate (messages: TelegramMessage[], i: number): boolean {
|
||||
if (i === 0) return true
|
||||
const current = new Date(messages[i].modifiedOn).toLocaleDateString()
|
||||
|
Loading…
Reference in New Issue
Block a user