Skip to content

Commit

Permalink
Merge pull request #18 from NivEz/add-chat-action
Browse files Browse the repository at this point in the history
Add chat action
  • Loading branch information
NivEz committed Mar 3, 2024
2 parents 3ce6bf8 + 84aed08 commit 2229628
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ jobs:
with:
telegram-api-token: ${{ secrets.TELEGRAM_API_TOKEN }}
telegram-chat-id: ${{ secrets.TELEGRAM_CHAT_ID }}
question: 'Updating Telenode. Which bump type you wish to perform?'
question: 'Updating Telenode. Which bump type you wish to perform? Default is "skip"'
options: '["prerelease", "patch", "minor", "major", "skip"]'
default-choice: 'skip'
message: 'The selected bump type is: %s'
timeout: 45
timeout: 30
wait-for-timeout-to-finish: true

bump:
Expand Down Expand Up @@ -78,11 +78,11 @@ jobs:
with:
telegram-api-token: ${{ secrets.TELEGRAM_API_TOKEN }}
telegram-chat-id: ${{ secrets.TELEGRAM_CHAT_ID }}
question: 'Should publish to npm?'
question: 'Should publish to npm? Default is "no"'
options: '["yes", "no"]'
default-choice: 'no'
message: 'The selected choice is: %s'
timeout: 45
timeout: 30
wait-for-timeout-to-finish: true

publish:
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ Lightweight Telegram API framework for Node.js
✅ Secret token support
<br>
✅ Long polling support
<br>
✅ Sending chat action

## Getting started

#### Complete example can be found [here](https://github.com/NivEz/movie-info-bot)

### Installation

```shellscript
Expand Down
23 changes: 23 additions & 0 deletions examples/chatAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Telenode = require('../src/bot');

const bot = new Telenode({
apiToken: process.env.API_TOKEN,
});

bot.createServer();

bot.onTextMessage('', async messageBody => {
await bot.sendTextMessage(
'I will be taking a random action for five seconds',
messageBody.chat.id,
);
const randomIdx = Math.floor(Math.random() * bot.validChatActions.length);
await bot.sendChatAction(messageBody.chat.id, bot.validChatActions[randomIdx]);
await sleep(5000);
await bot.sendTextMessage(
'The following API call will end the chat action',
messageBody.chat.id,
);
});

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
28 changes: 28 additions & 0 deletions src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ const axios = require('axios');
class Telenode {
#baseUrl;
#secretToken;
validChatActions = [
'typing',
'upload_photo',
'record_video',
'upload_video',
'record_voice',
'upload_voice',
'upload_document',
'choose_sticker',
'find_location',
'record_video_note',
'upload_video_note',
];

constructor({ apiToken, secretToken }) {
this.textHandlers = {};
Expand Down Expand Up @@ -186,6 +199,21 @@ class Telenode {
},
});
}

async sendChatAction(chatId, chatAction) {
if (!this.validChatActions.includes(chatAction)) {
throw new Error(
`Invalid chat action. Valid chat action should be one of: ${this.validChatActions.join(
', ',
)}`,
);
}
const url = this.#baseUrl + '/sendChatAction';
await axios.post(url, {
chat_id: chatId,
action: chatAction,
});
}
}

module.exports = Telenode;

0 comments on commit 2229628

Please sign in to comment.