Skip to content

Commit

Permalink
feat(contexts): implement send message for message event
Browse files Browse the repository at this point in the history
  • Loading branch information
negezor committed Oct 8, 2023
1 parent 53dffb6 commit e7f4f51
Showing 1 changed file with 83 additions and 2 deletions.
85 changes: 83 additions & 2 deletions packages/vk-io/src/structures/contexts/message-event.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Context, ContextFactoryOptions, ContextDefaultState } from './context';

import { pickProperties } from '../../utils/helpers';
import { kSerializeData } from '../../utils/constants';
import { IMessageContextSendOptions, MessageContext } from './message';

import { getRandomId, pickProperties } from '../../utils/helpers';
import { UpdateSource, kSerializeData } from '../../utils/constants';

export interface IMessageEventShowSnackbar {
type: 'show_snackbar';
Expand Down Expand Up @@ -107,6 +109,85 @@ export class MessageEventContext<S = ContextDefaultState>
});
}

/**
* Sends a message to the current dialog
*/
async send(
text: string | IMessageContextSendOptions,
params?: IMessageContextSendOptions,
): Promise<MessageContext<S>> {
const randomId = getRandomId();

const options = {
random_id: randomId,

...(
typeof text !== 'object'
? {
message: text,

...params,
}
: text
),
} as IMessageContextSendOptions;

if (this.$groupId !== undefined) {
options.peer_ids = this.peerId;
} else {
options.peer_id = this.peerId;
}

const rawDestination = await this.api.messages.send(options);

const destination = typeof rawDestination !== 'number'
? rawDestination[0] as {
peer_id : number;
message_id: number;
conversation_message_id: number;
error: number;
}
: {
peer_id: this.peer_id,
message_id: rawDestination,
conversation_message_id: 0,
};

const messageContext = new MessageContext<S>({
api: this.api,
upload: this.upload,
source: UpdateSource.WEBHOOK,
groupId: this.$groupId,
updateType: 'message_new',
state: this.state,
payload: {
client_info: this.clientInfo,
message: {
id: destination.message_id,
conversation_message_id: destination.conversation_message_id,

from_id: this.$groupId as unknown as number,
peer_id: destination.peer_id,

out: 1,
important: false,
random_id: randomId,

text: options.text,

date: Math.floor(Date.now() / 1000),

attachments: [],
},
},
});

// @ts-expect-error private method
messageContext.$filled = false;

return messageContext;
}

/**
* Returns the custom data
*/
Expand Down

0 comments on commit e7f4f51

Please sign in to comment.