@green-api/whatsapp-bot
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -11,2 +11,3 @@ { | ||
"dependencies": { | ||
"@green-api/v1-whatsapp-api-client": "^0.1.7", | ||
"@green-api/whatsapp-api-client": "^0.3.13", | ||
@@ -21,3 +22,3 @@ "debug": "^4.0.1", | ||
"deprecated": false, | ||
"description": "Whatatsapp bot library based on Telegraf", | ||
"description": "WhatsApp Bot for NodeJs", | ||
"devDependencies": { | ||
@@ -63,7 +64,6 @@ "@types/node": "^13.1.0", | ||
}, | ||
"scripts": { | ||
}, | ||
"scripts": {}, | ||
"type": "commonjs", | ||
"types": "./src/typings/index.d.ts", | ||
"version": "0.1.3" | ||
"version": "0.2.0" | ||
} |
@@ -9,3 +9,5 @@ # WhatsApp chat bot library for nodejs | ||
The WhatsApp chat bot designed for writing own chat bots. Users can interact with bots by sending them command messages in private or group chats. | ||
The WhatsApp chat bot designed for writing own chat bots. Users can interact with bots by sending them command messages in private or group chats. The bot uses Green-API ([green-api.com](https://green-api.com)) provider WhatsApp API protocol under hood to support WhatsApp. Green API whatsApp protocol exists in two versions: | ||
* V0 - requires plugged and active phone. The protocol provides reach WhatsApp API including messages, media, phone state, location etc. Have look at [whatsapp-api-client](https://github.com/green-api/whatsapp-api-client) wrapper REST protocol library for more information | ||
- V1 - phone-free protocol that requires only phone number without physical device. The protocol implements only limited part of WhatsApp API. See details here [v1-whatsapp-api-client](https://github.com/green-api/v1-whatsapp-api-client) | ||
@@ -22,3 +24,3 @@ ## Installation | ||
To use the WhatsApp Bot API, you first have to visit [green-api.com](https://green-api.com) and get free developer account. Green Api will give you id instance and api token, something like | ||
To use the WhatsApp Bot API, you first have to visit [green-api.com](https://green-api.com) and get free developer account for API-V0. Green Api will give you id instance and api token, something like | ||
``` | ||
@@ -29,2 +31,7 @@ ID_INSTANCE: "0000", | ||
If you want to get API-V1 protocol then you also have to visit [green-api.com](https://green-api.com) and choose Chat bot price option. Green API will take you a free trial period if you ask them. Access token looks like this: | ||
``` | ||
token = 'gr.abcdefg...' | ||
``` | ||
### 2. Add import | ||
@@ -41,2 +48,4 @@ | ||
### 3. Initiliaze new WhatsApp Bot with aquired account data | ||
For API-V0 protocol | ||
``` | ||
@@ -48,2 +57,6 @@ const bot = new WhatsAppBot({ | ||
``` | ||
For API-V1 protocol | ||
``` | ||
const bot = new WhatsAppBot(process.env.TOKEN_V1, {apiType: WhatsAppBot.GreenApiV1}) | ||
``` | ||
@@ -82,3 +95,3 @@ ### 3. Start coding | ||
There's some cool [examples too](docs/examples/). | ||
There's some cool V0-V1 api [examples too](docs/examples/). | ||
@@ -85,0 +98,0 @@ ## Documentation |
const replicators = require('./core/replicators') | ||
const ApiClient = require('./core/network/client') | ||
const whatsAppClient = require('@green-api/whatsapp-api-client') | ||
const ApiUtils = require('./api-utils') | ||
@@ -55,7 +56,17 @@ class GreenApiV0 extends ApiClient { | ||
const incomingMessageReceived = this.noty.body | ||
const text = (incomingMessageReceived.messageData.typeMessage == 'textMessage' ? incomingMessageReceived.messageData.textMessageData.textMessage : '') | ||
|| (incomingMessageReceived.messageData.typeMessage == 'quotedMessage' ? incomingMessageReceived.messageData.extendedTextMessageData.text : '') | ||
|| (incomingMessageReceived.messageData.typeMessage == 'extendedTextMessage' ? incomingMessageReceived.messageData.extendedTextMessageData.text : '') | ||
const incomingMsg = this.noty.body | ||
if (incomingMsg.typeWebhook !== 'incomingMessageReceived') { | ||
return [] | ||
} | ||
const text = (incomingMsg.messageData.typeMessage == 'textMessage' ? incomingMsg.messageData.textMessageData.textMessage : '') | ||
|| (incomingMsg.messageData.typeMessage == 'quotedMessage' ? incomingMsg.messageData.extendedTextMessageData.text : '') | ||
|| (incomingMsg.messageData.typeMessage == 'extendedTextMessage' ? incomingMsg.messageData.extendedTextMessageData.text : '') | ||
const image = (incomingMsg.messageData.typeMessage == 'imageMessage' ? incomingMsg.messageData.fileMessageData: '') | ||
const voice = (incomingMsg.messageData.typeMessage == 'audioMessage' ? incomingMsg.messageData.fileMessageData: '') | ||
const document = (incomingMsg.messageData.typeMessage == 'documentMessage' ? incomingMsg.messageData.fileMessageData: '') | ||
const video = (incomingMsg.messageData.typeMessage == 'videoMessage' ? incomingMsg.messageData.fileMessageData: '') | ||
const location = (incomingMsg.messageData.typeMessage == 'locationMessage' ? incomingMsg.messageData.locationMessageData: '') | ||
const contacts = (incomingMsg.messageData.typeMessage == 'contactMessage' ? inboundMessage.messageData.contactMessageData: '') | ||
@@ -67,7 +78,7 @@ const messageUpdate = { | ||
chat: { | ||
id: incomingMessageReceived.senderData.chatId, | ||
id: incomingMsg.senderData.chatId, | ||
type: 'channel', | ||
}, | ||
from: { | ||
first_name: incomingMessageReceived.senderData.senderName, | ||
first_name: incomingMsg.senderData.senderName, | ||
last_name: '', | ||
@@ -77,4 +88,4 @@ id: 0, | ||
}, | ||
message_id: incomingMessageReceived.idMessage, | ||
date: incomingMessageReceived.timestamp, | ||
message_id: incomingMsg.idMessage, | ||
date: incomingMsg.timestamp, | ||
caption_entities: [ | ||
@@ -90,2 +101,58 @@ { | ||
if (document) { | ||
messageUpdate.message.document = { | ||
file_id: document.downloadUrl, | ||
file_name: document.caption, | ||
//mime_type: document.mime_type, | ||
} | ||
} | ||
if (image) { | ||
messageUpdate.message.photo = [ | ||
{ | ||
file_id: image.downloadUrl, | ||
//width: | ||
//height: number; | ||
//file_size?: number; | ||
} | ||
] | ||
} | ||
if (voice) { | ||
messageUpdate.message.voice = { | ||
file_id: voice.downloadUrl, | ||
//duration: number; | ||
//mime_type: voice.mime_type, | ||
//file_size?: number; | ||
} | ||
} | ||
if (contacts) { | ||
messageUpdate.message.contact = { | ||
//phone_number: contacts; | ||
first_name: displayName, | ||
//last_name?: string; | ||
user_id: contacts.vcard, | ||
} | ||
} | ||
if (location) { | ||
messageUpdate.message.location = { | ||
longitude: location.longitude, | ||
latitude: location.latitude, | ||
} | ||
} | ||
if (video) { | ||
messageUpdate.message.video = { | ||
file_id: video.downloadUrl, | ||
//width: number; | ||
//height: number; | ||
//duration: number; | ||
//thumb?: PhotoSize; | ||
//mime_type?: string; | ||
//file_size?: number; | ||
} | ||
} | ||
return [ | ||
@@ -137,10 +204,4 @@ messageUpdate | ||
sendMessage (chatId, text, extra) { | ||
if (extra && extra.reply_markup && extra.reply_markup.keyboard) { | ||
const {reply_markup : { keyboard }} = extra | ||
keyboard[0].forEach(key => { | ||
text = text ? text + '\n' + key : key | ||
}) | ||
} | ||
return this.restAPI.message.sendMessage(chatId, null, text) | ||
const textWithKeys = ApiUtils.keyEmulation(extra, text) | ||
return this.restAPI.message.sendMessage(chatId, null, textWithKeys) | ||
} | ||
@@ -166,3 +227,3 @@ | ||
sendLocation (chatId, latitude, longitude, extra) { | ||
return this.callApi('sendLocation', { chat_id: chatId, latitude, longitude, ...extra }) | ||
return this.api.messages.sendLocation(chatId, null, 'nameLocation', 'address', latitude, longitude) | ||
} | ||
@@ -186,7 +247,10 @@ | ||
sendContact (chatId, phoneNumber, firstName, extra) { | ||
return this.callApi('sendContact', { chat_id: chatId, phone_number: phoneNumber, first_name: firstName, ...extra }) | ||
return this.restAPI.file.sendContact(chatId, null, { | ||
phoneContact: phoneNumber, | ||
firstName: firstName, | ||
}) | ||
} | ||
sendPhoto (chatId, photo, extra) { | ||
return this.callApi('sendPhoto', { chat_id: chatId, photo, ...extra }) | ||
return this.restAPI.file.sendFileByUrl(chatId, null, photo, 'photo') | ||
} | ||
@@ -199,7 +263,7 @@ | ||
sendDocument (chatId, document, extra) { | ||
return this.callApi('sendDocument', { chat_id: chatId, document, ...extra }) | ||
return this.restAPI.file.sendFileByUrl(chatId, null, document, 'document') | ||
} | ||
sendAudio (chatId, audio, extra) { | ||
return this.callApi('sendAudio', { chat_id: chatId, audio, ...extra }) | ||
return this.restAPI.file.sendFileByUrl(chatId, null, audio, 'video') | ||
} | ||
@@ -212,3 +276,3 @@ | ||
sendVideo (chatId, video, extra) { | ||
return this.callApi('sendVideo', { chat_id: chatId, video, ...extra }) | ||
return this.restAPI.file.sendFileByUrl(chatId, null, video, 'video') | ||
} | ||
@@ -225,3 +289,3 @@ | ||
sendVoice (chatId, voice, extra) { | ||
return this.callApi('sendVoice', { chat_id: chatId, voice, ...extra }) | ||
return this.restAPI.file.sendFileByUrl(chatId, null, voice, 'voice') | ||
} | ||
@@ -228,0 +292,0 @@ |
@@ -118,3 +118,3 @@ /** @format */ | ||
*/ | ||
constructor(token: V0_Options, options?: TelegrafOptions) | ||
constructor(token: V0_Options | string, options?: TelegrafOptions) | ||
@@ -121,0 +121,0 @@ /** |
const debug = require('debug')('@green-api/whatsapp-bot:core') | ||
const Telegram = require('./telegram') | ||
const GreenApiV0 = require('./green-api-v0') | ||
const GreenApiV1 = require('./green-api-v1') | ||
const Extra = require('./extra') | ||
@@ -223,2 +224,3 @@ const Composer = require('./composer') | ||
GreenApiV0, | ||
GreenApiV1, | ||
Stage, | ||
@@ -225,0 +227,0 @@ BaseScene, |
283525
37
7107
103
8
+ Added@green-api/v1-whatsapp-api-client@0.1.7(transitive)
+ Added@types/axios@0.14.4(transitive)
+ Addedaxios@0.21.4(transitive)