-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
207 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const { setJWT } = require('../../libs/database.cjs'); | ||
const { SERVER_HOST, SERVER_HOST_USERNAME, SERVER_HOST_PASSWORD } = require('../../environments/index.cjs'); | ||
const { generateTelegramHash } = require('../../libs/tg-crypto.cjs'); | ||
|
||
function registrationSuccessMessage() { | ||
return ` | ||
**Успешно зарегистрированы** ✅`.trim(); | ||
} | ||
|
||
/** | ||
* @description Ассистент детектирует пользователя | ||
* @param {any} bot - telegram bot | ||
* @param {any} message - telegram message | ||
* @returns {Promise<void>} | ||
*/ | ||
module.exports = async (bot, message) => { | ||
const body = { | ||
contact: { | ||
phoneNumber: message.contact.phone_number, | ||
firstName: message.contact.first_name, | ||
lastName: message.contact.last_name, | ||
userId: message.contact.user_id, | ||
}, | ||
authDate: message.date * 1000, | ||
hash: generateTelegramHash({ | ||
auth_date: message.date, | ||
contact: JSON.stringify(message.contact), | ||
}), | ||
}; | ||
try { | ||
// добавляем в запрос фото профиля | ||
const profilePhotos = await bot.getUserProfilePhotos(message.chat.id); | ||
if (profilePhotos.photos.length > 0) { | ||
const userPicture = await bot.getFileLink(profilePhotos.photos[0][0].file_id); | ||
body.picture = userPicture; | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
try { | ||
const response = await fetch(SERVER_HOST + '/registration', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': 'Basic ' + btoa(SERVER_HOST_USERNAME + ':' + SERVER_HOST_PASSWORD), | ||
}, | ||
body: JSON.stringify(body), | ||
}); | ||
if (!response.ok) { | ||
throw new Error('Произошла ошибка'); | ||
} | ||
const result = await response.text(); | ||
setJWT(Number(message.chat.id), result); | ||
await bot.deleteMessage(message.chat.id, message.message_id); | ||
await bot.sendMessage(message.chat.id, registrationSuccessMessage(), { | ||
parse_mode: 'MarkdownV2', | ||
message_effect_id: '5046509860389126442', // 🎉 | ||
reply_markup: { | ||
remove_keyboard: true, | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
return bot.sendMessage(message.chat.id, error.message, { | ||
message_effect_id: '5046589136895476101', // 💩 | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { | ||
NODE_ENV, | ||
DIALOGFLOW_CREDENTIALS, | ||
TELEGRAM_MINI_APP, | ||
SERVER_HOST, | ||
SERVER_HOST_USERNAME, | ||
SERVER_HOST_PASSWORD, | ||
TELEGRAM_TOKEN, | ||
TELEGRAM_DOMAIN, | ||
} = process.env; | ||
|
||
module.exports = { | ||
get IS_DEV() { | ||
return NODE_ENV?.toLowerCase()?.startsWith('dev'); | ||
}, | ||
TELEGRAM_MINI_APP: TELEGRAM_MINI_APP, | ||
TELEGRAM_TOKEN: TELEGRAM_TOKEN, | ||
TELEGRAM_DOMAIN: TELEGRAM_DOMAIN, | ||
TELEGRAM_MINI_APP_URL: 'https://t.me/gotois_bot/App', | ||
SERVER_HOST: SERVER_HOST, | ||
SERVER_HOST_USERNAME: SERVER_HOST_USERNAME, | ||
SERVER_HOST_PASSWORD: SERVER_HOST_PASSWORD, | ||
get DIALOGFLOW_CREDENTIALS() { | ||
return JSON.parse(DIALOGFLOW_CREDENTIALS); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.