Skip to content

Commit

Permalink
Merge pull request #130 from green-api/SW-3014
Browse files Browse the repository at this point in the history
SW-3014
  • Loading branch information
Amele9 authored Mar 7, 2024
2 parents 5f78102 + 85036a4 commit df91fdb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 36 additions & 6 deletions src/utils/GroupAPI.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'
import axios from 'axios';
import CommonUtils from './CommonUtils.js'
import fs from "fs";
import mime from "mime";

class GroupAPI {

Expand All @@ -10,19 +12,16 @@ class GroupAPI {
/**
*
* @param {String} groupName
* @param {Array} chatIds
* @param {Array} phones
* @param {Array} chatIds
*/
async createGroup(groupName, chatIds, phones) {
async createGroup(groupName, chatIds) {
CommonUtils.validateString('groupName', groupName);
CommonUtils.validateArray('chatIds', chatIds);
CommonUtils.validateArray('phones', phones);

const method = 'createGroup';
const postData = {
'groupName': groupName,
'chatIds': chatIds,
'phones': phones,
'chatIds': chatIds
}
const response = await axios.post(CommonUtils.generateMethodURL(this._restAPI.params, method), postData);
return response.data;
Expand Down Expand Up @@ -154,6 +153,37 @@ class GroupAPI {
return response.data;
}

/**
* @param {String} filePath
* @param {String} groupId
*/
async setGroupPicture(groupId, filePath) {
CommonUtils.validateString("filePath", filePath)
CommonUtils.validateString('groupId', groupId);

const method = "setGroupPicture";

const fileStream = fs.createReadStream(filePath)
const fileData = [];

for await (const data of fileStream) {
fileData.push(data);
}

const blob = new Blob(fileData, { type: 'image/jpeg' });

const formData = new FormData()
formData.append('groupId', groupId)
formData.append('file', blob, "group_avatar.jpg")

const response = await axios({
method: "post",
url: CommonUtils.generateMethodURL(this._restAPI.params, method),
data: formData,
headers: {"Content-Type": "image/jpeg"}
})
return response.data;
}
}

export default GroupAPI;
17 changes: 15 additions & 2 deletions src/utils/MessageAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class MessageAPI {
* @param {Number} phoneNumber - receiver phone number using international format without + sign.
* Mandatory if chatId is empty
* @param {String} message - text message
* @param {boolean} linkPreview - allow preview
* @param {String} quotedMessageId - id of message
*/
async sendMessage(chatId, phoneNumber, message) {
async sendMessage(chatId, phoneNumber, message, quotedMessageId = null, linkPreview = null) {
CommonUtils.validateChatIdPhoneNumber(chatId, phoneNumber);
CommonUtils.validateString('message', message);

Expand All @@ -26,6 +28,13 @@ class MessageAPI {
'message': message,
}

if (quotedMessageId !== null) {
postData['quotedMessageId'] = quotedMessageId
}
if (linkPreview !== null) {
postData['linkPreview'] = linkPreview
}

this.addChadIdParam(postData, chatId)
this.addPhoneParam(postData, phoneNumber)

Expand Down Expand Up @@ -303,7 +312,7 @@ class MessageAPI {
/**
* Returns history of chat
*/
async getChatHistory(chatId) {
async getChatHistory(chatId, count = null) {
CommonUtils.validateChatIdPhoneNumber(chatId, undefined);

const method = 'getChatHistory';
Expand All @@ -312,6 +321,10 @@ class MessageAPI {
'chatId': chatId,
}

if (count !== null && count > 0) {
postData['count'] = count;
}

this.addChadIdParam(postData, chatId)

const response = await axios.post(CommonUtils.generateMethodURL(this._restAPI.params, method), postData);
Expand Down

0 comments on commit df91fdb

Please sign in to comment.