Send Messages, Photos, and Documents to Telegram using Telegram Bot API with node-telegram-bot-api module.
npm install moleculer-telegram-bot --saveBefore use the example below, please set the
TELEGRAM_TOKENenvironment variables and optionalTELEGRAM_TARGETfor default target.
const { ServiceBroker } = require('moleculer');
const TelegramService = require('moleculer-telegram-bot');
// Create broker
const broker = new ServiceBroker({ logger: console });
// Load my service
broker.createService({
name: 'telegram',
mixins: [TelegramService()]
});
// Start server
broker.start().then(() => {
broker
.call('telegram.sendMessage', { message: 'Hello Telegram!' })
.then(res => console.log('Telegram message sent.'))
.catch(console.error);
});| Property | Type | Default | Description |
|---|---|---|---|
telegramToken |
String |
required | Telegram Bot API Token. Visit How do I create a bot?. |
telegramTarget |
String | Number |
- | Telegram chat id as default target |
Send a Telegram Message
| Property | Type | Default | Description |
|---|---|---|---|
message |
String |
required | Message text |
to |
String | Number |
- | Message target (required if default target not set) |
parse_mode |
String |
- | Optional parse mode |
disable_web_page_preview |
Boolean |
- | Optional disable web page preview |
disable_notification |
Boolean |
- | Optional disable notification |
reply_to_message_id |
Number |
- | Optional reply to message id |
reply_markup |
Any |
- | Optional reply markup |
Send a Telegram Photo using URL.
| Property | Type | Default | Description |
|---|---|---|---|
photo |
String |
required | Photo URL |
to |
String | Number |
- | Photo target (required if default target not set) |
parse_mode |
String |
- | Optional parse mode |
caption |
String |
- | Optional photo caption |
disable_notification |
Boolean |
- | Optional disable notification |
reply_to_message_id |
Number |
- | Optional reply to message id |
reply_markup |
Any |
- | Optional reply markup |
fileOpts.filename |
String |
- | Optional filename |
fileOpts.contentType |
String |
- | Optional file content type |
Send a Telegram Document using URL.
| Property | Type | Default | Description |
|---|---|---|---|
doc |
String |
required | Document URL |
to |
String | Number |
- | Document target (required if default target not set) |
parse_mode |
String |
- | Optional parse mode |
caption |
String |
- | Optional document caption |
disable_notification |
Boolean |
- | Optional disable notification |
reply_to_message_id |
Number |
- | Optional reply to message id |
reply_markup |
Any |
- | Optional reply markup |
fileOpts.filename |
String |
- | Optional filename |
fileOpts.contentType |
String |
- | Optional file content type |
Send a message
| Property | Type | Default | Description |
|---|---|---|---|
to |
String | Number |
- | Message target |
message |
String |
- | Body of the message |
opts |
TelegramBot.SendMessageOptions |
- | Send message options |
Send a photo
| Property | Type | Default | Description |
|---|---|---|---|
to |
String | Number |
- | Message target |
photo |
String | Stream | Buffer |
- | Photo to send |
opts |
TelegramBot.SendPhotoOptions |
- | Send photo options |
fileOpts |
TelegramBot.fileOpts |
- | Send photo file options |
Send a document
| Property | Type | Default | Description |
|---|---|---|---|
to |
String | Number |
- | Message target |
doc |
String | Stream | Buffer |
- | Document to send |
opts |
TelegramBot.SendDocumentOptions |
- | Send document options |
fileOpts |
TelegramBot.fileOpts |
- | Send document file options |
npm testIn development with watching
npm run ciThe project is available under the MIT license.