Skip to content

Commit

Permalink
Improve telegram integration (GladysAssistant#840)
Browse files Browse the repository at this point in the history
* Fix GladysAssistant#730 : Send a message when Telegram integration was configured with success

* Fix GladysAssistant#607 : In Telegram service, use the chat.id to answer to a user, not the from.id

* .gitignore .DS_Store

* Fix telegram tests
  • Loading branch information
Pierre-Gilles authored and NickDub committed Aug 7, 2020
1 parent 7dff879 commit 0820d57
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ coverage
# VSCode
.vscode
.tmp
.DS_Store
4 changes: 4 additions & 0 deletions server/services/telegram/lib/message.linkUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ async function linkUser(apiKey, chatId) {
telegram_user_id: chatId,
});
logger.debug(`Telegram user linked with success.`);
// sending message to confirm
await this.send(chatId, {
text: '👋',
});
}

module.exports = {
Expand Down
5 changes: 4 additions & 1 deletion server/services/telegram/lib/message.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ const { EVENTS } = require('../../../utils/constants');
async function newMessage(msg) {
logger.debug(`new message from telegram, ${msg.text}`);
logger.debug(msg);

const telegramUserId = msg.from.id;
const telegramChatId = msg.chat.id;

if (msg.text.startsWith('/start')) {
const splitted = msg.text.split(' ');
await this.linkUser(splitted[1], telegramUserId);
return null;
}

const user = await this.gladys.user.getByTelegramUserId(telegramUserId);

const message = {
source: 'telegram',
source_user_id: telegramUserId,
source_user_id: telegramChatId,
user_id: user.id,
user,
language: user.language,
Expand Down
6 changes: 6 additions & 0 deletions server/test/services/telegram/lib/messageHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ describe('Telegram.message', () => {
from: {
id: '89798374Z7',
},
chat: {
id: '89798374Z7',
},
text: 'Hey',
date: new Date(),
});
Expand All @@ -44,6 +47,9 @@ describe('Telegram.message', () => {
from: {
id: '89798374Z7',
},
chat: {
id: '89798374Z7',
},
text: '/start testapikey',
date: new Date(),
});
Expand Down

0 comments on commit 0820d57

Please sign in to comment.