ICQ New Bot SDK for Node.js.
This client is build on top of the ICQ New bot API.
The full description of the bot api can be found on icq.com/botapi/ or agent.mail.ru/botapi/.
Create your own bot by sending the /newbot command to Metabot and follow the instructions.
$ npm install icq-bot-sdk --save
const ICQClient = require('icq-bot-sdk').default;
const icqBot = ICQClient({
token: process.env.ICQ_BOT_TOKEN, // your bot token goes here
});
icqBot.on('error', (error) => {
console.log(error);
icq.stop(); // stop event loop or handle error some other way
});
icqBot.on('newMessage', (result) => {
// do something with the result
});
icqBot.on('all', (result) => {
// handle every new event here
});
icqBot.startPolling(); // start event loop
const ICQClient = require('icq-bot-sdk').default;
const eventIdStorage = {
id: 0,
async getId() { return this.id; },
async setId(id) { this.id = id; },
},
const icqBot = ICQClient({
token: process.env.ICQ_BOT_TOKEN, // your bot token goes here
pollTime: 2,
timeout: 3,
eventIdStorage,
});
Create a new instance of the ICQ Bot client.
Name | Type | Description | Notes |
---|---|---|---|
token | String | Secret token that you've recieved from the Metabot. | [required] |
pollTime | Integer | Time for keeping the connection alive (secs) during events polling. | [optional] [default to 1] |
timeout | Integer | Time for sleeping between long polling requests to the server (secs). | [optional] [default to 5] |
eventIdStorage | Object | Object with two async methods getId and setId to track the last known server event id. |
[optional] [default to the build-in in-memory storage] |
Method | Description |
---|---|
startPolling | Start polling events from the server |
stop | Stop polling |
on | Subscribe to the event from server |
off | Unsubscribe from the event from server |
chats.blockUser | Block a user in a chat |
chats.getAdmins | Get the list of admins |
chats.getBlockedUsers | Get the list of all users that have been banned in the chat |
chats.getInfo | Get the info about a chat |
chats.getMembers | Get the list of all members of a chat |
chats.getPendingUsers | Get the list of users waiting to be accepted into chat. |
chats.pinMessage | Pin a missage in the chat |
chats.resolvePending | Decide whether to accept or not pending users. |
chats.sendActions | Send an action to a chat |
chats.setAbout | Change the description of a chat |
chats.setRules | Change the rules for the chat |
chats.setTitle | Change the title of a chat |
chats.unblockUser | Unblock a user in a chat |
chats.unpinMessage | Unpin a message in a chat |
events.get | Get events |
files.getInfo | Get info about a file |
messages.answerCallbackQuery | A button click handler |
messages.deleteMessages | Delete messages |
messages.editText | Edit a message |
messages.sendFile | Send a previously loaded file |
messages.sendVoice | Send a previously uploaded voice message |
messages.sendText | Send a message |
messages.uploadAndSendFile | Upload and send a new file |
messages.uploadAndSendVoice | Upload and send a new voice message |
self.get | Get info about the bot |
icqBot
.startPolling({
pollTime: 1,
timout: 3,
});
Start polling events from the server.
Name | Type | Description | Notes |
---|---|---|---|
pollTime | Integer | Time for keeping the connection alive (secs). | [optional] [default to the value specified during the construction] |
timeout | Integer | Time for sleeping between requests to the server (secs). | [optional] [default to the value specified during the construction] |
icqBot.stop();
Stop polling events from the server.
icqBot
.on('newMessage', function handler({ payload: { chat }}) {
this.messages.sendVoice({
chatId: chat.chatId,
text: 'Some text',
})
});
Add event listener to an event from the server.
The instance of EventEmitter is actually in the prototype chain of the icqBot
object. It means it's possible to use any method on EventEmitter
including the on
method.
The list of available events is: newMessage
, editedMessage
, deletedMessage
, pinnedMessage
, unpinnedMessage
, newChatMembers
, leftChatMembers
, callbackQuery
The event object passed to the event handler is depends on the type of the event. See icq.com/botapi/ for more details.
icqBot
.off('newMessage', someHandler);
Remove event listener to an event from the server.
The instance of EventEmitter is actually in the prototype chain of the icqBot
object. It means it's possible to use any method on EventEmitter
including the off
method.
The list of available events is: newMessage
, editedMessage
, deletedMessage
, pinnedMessage
, unpinnedMessage
, newChatMembers
, leftChatMembers
, callbackQuery
icqBot
.chats.blockUser({
chatId: 'some-id',
userId: 'some-id',
delLastMessages: true,
})
.then(handleResult)
.catch(handleError);
Block a user in a chat
If the user was a member of the chat, he is going to be removed from the chat and banned. It is also possible to delete the user's last messages delLastMessages
.
The bot has to be an admin of the chat in order to be able to perform these actions.
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, group id or channel id. Id can be obtained from events (chatId ). |
[required] |
userId | String | Unique user nick or id. | [required] |
delLastMessages | Boolean | Delete last messages of the given user. | [optional] [default to false] |
{
"ok": true // required
}
icqBot
.chats.getAdmins({
chatId: 'some-id',
})
.then(handleResult)
.catch(handleError);
Get the list of admins
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, group id or channel id. Id can be obtained from events (chatId ). |
[required] |
The list of admins.
{
"admins": [
{
"userId": "string", // required
"creator": true // optional
}
]
}
icqBot
.chats.getBlockedUsers({
chatId: 'some-id',
})
.then(handleResult)
.catch(handleError);
Get the list of all users that have been banned in the chat
The bot has to be an admin of the chat in order to be able to perform this action.
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, group id or channel id. Id can be obtained from events (chatId ). |
[required] |
The list of blocked users.
{
"users": [
{
"userId": "string"
}
]
}
icqBot
.chats.getInfo({
chatId: 'some-id',
})
.then(handleResult)
.catch(handleError);
Get the info about a chat
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, chat id or user id. Id could be obtained from incoming events (поле chatId ). |
[required] |
The info about about the chat.
{
"type": "private",
"firstName": "Name",
"lastName": "Surname",
"nick": "nickname",
"about": "Information about user",
"isBot": true
}
icqBot
.chats.getMembers({
chatId: 'some-id',
cursor: 'cursor',
})
.then(handleResult)
.catch(handleError);
Get the list of all members of the chat.
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, chat id or user id. Id could be obtained from incoming events (поле chatId ). |
[required] |
cursor | String | Identifier for obtaining the continuation of the users' list. The cursor can be obtained from the first/previous getMembers request. |
[optional] [default to null] |
The list of all members of the chat.
{
"members": [
{
"userId": "string",
"creator": true
}
]
}
icqBot
.chats.getPendingUsers({
chatId: 'some-id',
})
.then(handleResult)
.catch(handleError);
Get the list of users waiting to be accepted into chat.
The bot has to be an adming of the chat in order to be able to perform this action
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, chat id or user id. Id could be obtained from incoming events (поле chatId ). |
[required] |
The list of pending users.
{
"users": [
{
"userId": "string"
}
]
}
icqBot
.chats.pinMessage({
chatId: 'some-id',
msgId: 156,
})
.then(handleResult)
.catch(handleError);
Pin a missage in the chat
The bot has to be an adming of the chat in order to be able to perform this action
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, chat id or user id. Id could be obtained from incoming events (поле chatId ). |
[required] |
msgId | Integer | Message id. | [required] |
{
"ok": true // required
}
icqBot
.chats.resolvePending({
chatId: 'some-id',
userId: 'some-id',
approve: true,
})
.then(handleResult)
.catch(handleError);
Decide whether to accept or not pending users.
One of the params userId
or everyone
has to be sent. You can`t send both these params.
The bot has to be an adming of the chat in order to be able to perform this action.
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, chat id or user id. Id could be obtained from incoming events (поле chatId ). |
[required] |
approve | Boolean | Approve or reject. | [required] |
userId | String | User nick or id who is waiting for joining the chat. Can't be sent with the everyone param. |
[optional] [default to null] |
everyone | Boolean | Decision about all the users waiting for joining the chat. Can't be sent with the userId param. |
[optional] [default to null] |
{
"ok": true // required
}
icqBot
.chats.sendActions({
chatId: 'some-id',
actions: ['looking', 'typing'],
})
.then(handleResult)
.catch(handleError);
Send an action to a chat
The method have to be called on every change of the action or every 10 seconds if the action hasn't been changed. After the request with action has been sent there is no point in sending message about the empty action.
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, chat id or user id. Id could be obtained from incoming events (поле chatId ). |
[required] |
actions | List of actions' strings | Current actions in the chat. Send empty if all actions have been finished. | [required] [enum: looking, typing] |
{
"ok": true // required
}
icqBot
.chats.setAbout({
chatId: 'some-id',
about: 'Info about the chat',
})
.then(handleResult)
.catch(handleError);
Change the description of a chat
The bot has to be an adming of the chat in order to be able to perform this action
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, chat id or user id. Id could be obtained from incoming events (поле chatId ). |
[required] |
about | String | Chat description | [required] |
{
"ok": true // required
}
icqBot
.chats.setRules({
chatId: 'some-id',
rules: 'Rules of the chat',
})
.then(handleResult)
.catch(handleError);
Change the rules for the chat
The bot has to be an adming of the chat in order to be able to perform this action
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, chat id or user id. Id could be obtained from incoming events (поле chatId ). |
[required] |
rules | String | Chat rules | [required] |
{
"ok": true // required
}
icqBot
.chats.setTitle({
chatId: 'some-id',
title: 'Title of the chat',
})
.then(handleResult)
.catch(handleError);
Change the title of a chat
The bot has to be an adming of the chat in order to be able to perform this action
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, chat id or user id. Id could be obtained from incoming events (поле chatId ). |
[required] |
title | String | Chat title | [required] |
{
"ok": true // required
}
icqBot
.chats.unblockUser({
chatId: 'some-id',
userId: 'some-id',
})
.then(handleResult)
.catch(handleError);
Unblock a user in a chat
The bot has to be an adming of the chat in order to be able to perform this action
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, chat id or user id. Id could be obtained from incoming events (поле chatId ). |
[required] |
userId | String | Unique user nick or id. | [required] |
{
"ok": true // required
}
icqBot
.chats.unpinMessage({
chatId: 'some-id',
msgId: 149,
})
.then(handleResult)
.catch(handleError);
Unpin a message in a chat
The bot has to be an adming of the chat in order to be able to perform this action
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, chat id or user id. Id could be obtained from incoming events (поле chatId ). |
[required] |
msgId | Integer | Message id. | [required] |
{
"ok": true // required
}
icqBot
.events.get({
lastEventId: 35,
pollTime: 1,
})
.then(handleResult)
.catch(handleError);
Get events
Every event has an identifier eventId
. When you call the method you have to send the biggest knonw event id in the parameter lastEventId
. For the first call set this param to 0. If there are no events on the server at the moment of the call, the connection is going to be kept alive. As soon as an event has occured the server will send it back and close the connection. If after pollTime
seconds of waiting no events have been emitted, the server will return an empty array of events
.
Name | Type | Description | Notes |
---|---|---|---|
lastEventId | Integer | Id of the last known event. | [required] |
pollTime | Integer | Time for keeping the connection alive (secs). | [required] |
A list of events since lastEventId.
{
"events": [
{
"eventId": 1,
"type": "newMessage",
"payload": {
"msgId": "57883346846815032",
"chat": {
"chatId": "[email protected]",
"type": "channel",
"title": "The best channel"
},
"from": {
"userId": "1234567890",
"firstName": "Name",
"lastName": "SurName"
},
"timestamp": 1546290000,
"text": "Hello!",
"parts": [
{
"type": "sticker",
"payload": {
"fileId": "2IWuJzaNWCJZxJWCvZhDYuJ5XDsr7hU"
}
}
]
}
}
]
}
icqBot
.files.getInfo({
fileId: 'some-id',
})
.then(handleResult)
.catch(handleError);
Get info about a file
Name | Type | Description | Notes |
---|---|---|---|
fileId | String | Id of the previously uploaded file. | [required] |
Information about a file.
{
"type": "video",
"size": 20971520,
"filename": "VIDEO.mkv",
"url": "https://example.com/get/88MfCLBHphvOAOeuzYhZfW5b7bcfa31ab"
}
icqBot
.messages.answerCallbackQuery({
chatId: 'some-id',
userId: 'Some text',
showAlert: true,
})
.then(handleResult)
.catch(handleError);
A button click handler
Use the method whenever the event [callbackQuery] is received
Name | Type | Description | Notes |
---|---|---|---|
queryId | String | Id of the callback query received by the bot | [required] |
text | String | Text that is going to be shown to a user. If not set, nothing will be shown. | [optional] [default to null] |
showAlert | Boolean | If it has been set to true , the alert is going to be shown instead of notification. |
[optional] [default to null] |
url | String | URL to open by a client app | [optional] [default to null] |
{
"ok": true // required
}
icqBot
.messages.deleteMessages({
chatId: 'some-id',
msgId: ['some-id'],
})
.then(handleResult)
.catch(handleError);
Delete messages
There are some limitations when the method could be used. See the official docs for more details
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, group id or channel id. Id can be obtained from events (chatId ). |
[required] |
msgId | List of message ids | Messages ids. | [required] |
{
"ok": true // required
}
icqBot
.messages.editText({
chatId: 'some-id',
msgId: 'some-id',
text: 'Some text',
})
.then(handleResult)
.catch(handleError);
Edit a message
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, group id or channel id. Id can be obtained from events (chatId ). |
[required] |
msgId | Integer | Message id. | [required] |
text | String | Message text. A user can be tagged in a format @[userId]. | [required] |
{
"ok": true // required
}
icqBot
.messages.sendFile({
chatId: 'some-id',
fileId: 'some-id',
})
.then(handleResult)
.catch(handleError);
Send a previously loaded file
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, group id or channel id. Id can be obtained from events (chatId ). |
[required] |
fileId | String | Id of the previously uploaded file. | [required] |
caption | String | File caption. | [optional] [default to null] |
replyMsgId | List of message ids | Id of a quoted message. Can't be sent with forwardChatId and forwardMsgId params. |
[optional] [default to null] |
forwardChatId | String | Chat id from which a message is going to be forwarded. Set only with the forwardMsgId . Can't be set with the replyMsgId param. |
[optional] [default to null] |
forwardMsgId | List of message ids | Message id to forwaded. Set only with the forwardChatId . Can't be set with the replyMsgId param. |
[optional] [default to null] |
{
"msgId": "57883346846815032",
"ok": true
}
icqBot
.messages.sendVoice({
chatId: 'some-id',
fileId: 'some-id',
})
.then(handleResult)
.catch(handleError);
Send a previously uploaded voice message
The format of a message should be aac, ogg or m4a.
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, group id or channel id. Id can be obtained from events (chatId ). |
[required] |
fileId | String | Id of the previously uploaded file. | [required] |
replyMsgId | List of message ids | Id of a quoted message. Can't be sent with forwardChatId and forwardMsgId params. |
[optional] [default to null] |
forwardChatId | String | Chat id from which a message is going to be forwarded. Set only with the forwardMsgId . Can't be set with the replyMsgId param. |
[optional] [default to null] |
forwardMsgId | List of message ids | Message id to forwaded. Set only with the forwardChatId . Can't be set with the replyMsgId param. |
[optional] [default to null] |
{
"msgId": "57883346846815032",
"ok": true
}
icqBot
.messages.sendText({
chatId: 'some-id',
text: 'Some text',
})
.then(handleResult)
.catch(handleError);
Send a message
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, group id or channel id. Id can be obtained from events (chatId ). |
[required] |
text | String | Message text. A user can be tagged in a format @[userId]. | [required] |
replyMsgId | List of message ids | Id of a quoted message. Can't be sent with forwardChatId and forwardMsgId params. |
[optional] [default to null] |
forwardChatId | String | Chat id from which a message is going to be forwarded. Set only with the forwardMsgId . Can't be set with the replyMsgId param. |
[optional] [default to null] |
forwardMsgId | List of message ids | Message id to forwaded. Set only with the forwardChatId . Can't be set with the replyMsgId param. |
[optional] [default to null] |
{
"msgId": "57883346846815032",
"ok": true
}
icqBot
.messages.uploadAndSendFile({
chatId: 'some-id',
file: someFile,
})
.then(handleResult)
.catch(handleError);
Upload and send a new file
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, group id or channel id. Id can be obtained from events (chatId ). |
[required] |
caption | String | File caption. | [optional] [default to null] |
replyMsgId | List of message ids | Id of a quoted message. Can't be sent with forwardChatId and forwardMsgId params. |
[optional] [default to null] |
forwardChatId | String | Chat id from which a message is going to be forwarded. Set only with the forwardMsgId . Can't be set with the replyMsgId param. |
[optional] [default to null] |
forwardMsgId | List of message ids | Message id to forwaded. Set only with the forwardChatId . Can't be set with the replyMsgId param. |
[optional] [default to null] |
file | File | File | [optional] [default to null] |
{
"fileId": "0dC76vcKS3XZOtG5DVs9y15d1daefa1ae",
"msgId": "57883346846815032",
"ok": true
}
icqBot
.messages.uploadAndSendVoice({
chatId: 'some-id',
file: someFile,
})
.then(handleResult)
.catch(handleError);
Upload and send a new voice message
The format of a message should be aac, ogg or m4a.
Name | Type | Description | Notes |
---|---|---|---|
chatId | String | Unique nick, group id or channel id. Id can be obtained from events (chatId ). |
[required] |
replyMsgId | List of message ids | Id of a quoted message. Can't be sent with forwardChatId and forwardMsgId params. |
[optional] [default to null] |
forwardChatId | String | Chat id from which a message is going to be forwarded. Set only with the forwardMsgId . Can't be set with the replyMsgId param. |
[optional] [default to null] |
forwardMsgId | List of message ids | Message id to forwaded. Set only with the forwardChatId . Can't be set with the replyMsgId param. |
[optional] [default to null] |
file | File | File | [optional] [default to null] |
{
"fileId": "0dC76vcKS3XZOtG5DVs9y15d1daefa1ae",
"msgId": "57883346846815032",
"ok": true
}
icqBot
.self.get()
.then(handleResult)
.catch(handleError);
Get info about the bot
The method can be used to check if token is valid.
Information about the bot.
{
"userId": "747432131",
"nick": "test_api_bot",
"firstName": "TestBot",
"about": "The description of the bot",
"photo": [
{
"url": "https://example.com/expressions/getAsset?f=native&type=largeBuddyIcon&id=0110379ad781bcc4a969242f1f5a93144362"
}
],
"ok": true
}
Please take a moment to read our contributing guidelines if you haven't yet done so.