Skip to content

Commit 9bb8ab2

Browse files
feat: implement Telegram API integration using fetch
- Replace mock implementation with actual Telegram Bot API calls - Use Node.js fetch to send messages via https://api.telegram.org/bot - Add proper error handling for API responses - Include detailed error messages in response - Fix linter issues with property shorthand
1 parent c749aa8 commit 9bb8ab2

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

dapp/src/telegramService.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,46 @@ async function sendTelegram({
1111
if (!message || message.trim() === '')
1212
throw new Error('Message content is required');
1313

14-
1514
const messageToSend = `Message from: ${senderName}\n${message}`;
1615

17-
console.log('removed lib for testing');
16+
try {
17+
const response = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {
18+
method: 'POST',
19+
headers: {
20+
'Content-Type': 'application/json',
21+
},
22+
body: JSON.stringify({
23+
chat_id: chatId,
24+
text: messageToSend,
25+
parse_mode: 'HTML'
26+
})
27+
});
28+
29+
const result = await response.json();
30+
31+
if (!response.ok) {
32+
console.error('Telegram API Error:', result);
33+
return {
34+
message: 'Failed to send Telegram message.',
35+
status: response.status,
36+
error: result.description
37+
};
38+
}
1839

19-
return {
20-
message: 'Your telegram message has been sent successfully.',
21-
status: 200,
22-
};
40+
console.log('Message successfully sent by Telegram bot.');
41+
return {
42+
message: 'Your telegram message has been sent successfully.',
43+
status: 200,
44+
result
45+
};
46+
} catch (error) {
47+
console.error('Failed to send Telegram message:', error);
48+
return {
49+
message: 'Failed to send Telegram message.',
50+
status: 500,
51+
error: error.message
52+
};
53+
}
2354
}
2455

2556
export default sendTelegram;

0 commit comments

Comments
 (0)