UNPKG

vvlad1973-telegram-framework

Version:
255 lines (185 loc) 7.06 kB
'use strict' import { getMessageType, getFileId } from '../helpers/utils.js'; export class Message { constructor(data, botInstance) { if (data?.message_id) { this.sender = botInstance; Object.assign(this, data); Object.defineProperties(this, { type: { get: () => getMessageType(this) }, fileId: { get: () => getFileId(this) } }); } else return undefined; } answerMessage(text, options) { return this.sender.sendMessage(this.chat.id, text, options); } replyMessage(text, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.sendMessage(this.chat.id, text, options); } answerPhoto(photo, caption, options) { return this.sender.sendPhoto(this.chat.id, photo, caption, options = {}); } replyPhoto(photo, caption, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.sendPhoto(this.chat.id, photo, caption, options = {}); } answerDice(options) { return this.sender.sendDice(this.chat.id, options); } replyDice(options) { options['reply_to_message_id'] = this.message_id; return this.sender.sendDice(this.chat.id, options); } delete() { return this.sender.deleteMessage(this.chat.id, this.message_id); } pin(options) { return this.sender.pinChatMessage(this.chat.id, this.message_id, options); } unpin() { return this.sender.unpinChatMessage(this.chat.id, this.message_id); } unpinAllChatMessages() { return this.sender.unpinAllChatMessages(this.chat.id); } editText(text, options = {}) { options['chat_id'] = this.chat.id; options['message_id'] = this.message_id; if (options.preserve_reply_markup) { if (this.reply_markup) options['reply_markup'] = this.reply_markup; delete options.preserve_reply_markup; } if (this.text && this.text !== text) { if (options.caption_entity) { options['entity'] = options['caption_entity']; delete options['caption_entity']; } return this.sender.editMessageText(text, options); } else if (this.caption && this.caption !== text) { if (options.entity) { options['caption_entity'] = options['entity']; delete options['entity']; } return this.sender.editMessageCaption(text, options); } else return Promise.resolve(false); } editReplyMarkup(options = {}) { options['chat_id'] = this.chat.id; options['message_id'] = this.message_id; if (options['reply_markup'] !== this.reply_markup) return this.sender.editMessageReplyMarkup(options) else return false; } editMedia(media, options = {}) { if ( this.audio || this.video || this.photo || this.document ) { options['chat_id'] = this.chat.id; options['message_id'] = this.message_id; if (options.preserve_reply_markup) { if (this.reply_markup) options['reply_markup'] = this.reply_markup; delete options.preserve_reply_markup; } return this.sender.editMessageMedia(media, options); } else return Promise.resolve(false); } answerVideo(video, caption, options) { return this.sender.sendVideo(this.chat.id, video, caption, options); } replyVideo(video, caption, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.sendVideo(this.from.id, video, caption, options); } answerContact(phoneNumber, firstName, lastName, options) { return this.sender.sendContact( this.chat.id, phoneNumber, firstName, lastName, options ); } replyContact(phoneNumber, firstName, lastName, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.sendContact( this.chat.id, phoneNumber, firstName, lastName, options ); } answerAudio(audio, caption, options) { return this.sender.sendAudio(this.chat.id, audio, caption, options); } replyAudio(audio, caption, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.sendAudio(this.chat.id, audio, caption, options); } answerVoice(voice, caption, options) { return this.sender.sendVoice(this.chat.id, voice, caption, options); } replyVoice(voice, caption, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.sendVoice(this.chat.id, voice, caption, options); } answerVideoNote(video_note, options) { return this.sender.sendVideoNote(this.chat.id, video_note, options); } replyVideoNote(video_note, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.sendVideoNote(this.chat.id, video_note, options); } answerMediaGroup(media, options) { return this.sender.sendMediaGroup(this.chat.id, media, options); } replyMediaGroup(media, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.sendMediaGroup(this.chat.id, media, options); } answerSticker(sticker, options) { return this.sender.sendSticker(this.chat.id, sticker, options); } replySticker(sticker, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.sendSticker(this.chat.id, sticker, options); } answerDocument(document, caption, options) { return this.sender.sendDocument(this.chat.id, document, caption, options); } replyDocument(document, caption, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.sendDocument(this.chat.id, document, caption, options); } answerAnimation(animation, caption, options) { return this.sender.sendAnimation(this.chat.id, animation, caption, options); } replyAnimation(animation, caption, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.sendAnimation(this.chat.id, animation, caption, options); } answerChatAction(action) { return this.sender.sendChatAction(this.chat.id, action); } forward(chatId, options) { return this.sender.forwardMessage(chatId, this.chat.id, this.message_id, options); } copy(chatId, options) { return this.sender.copyMessage(chatId, this.chat.id, this.message_id, options); } replyCopyMessage(chatId, options = {}) { options['reply_to_message_id'] = this.message_id; return this.sender.copyMessage(chatId, this.chat.id, this.message_id, options); } } // export function getMediaType(object) { // let result; // if (object.photo) result = 'photo'; // else if (object.video) result = 'video'; // else if (object.audio) result = 'audio'; // else if (object.document) result = 'document'; // return result; // }