Skip to content

Commit

Permalink
(fix) Force names to send synchronously
Browse files Browse the repository at this point in the history
Closes #60
  • Loading branch information
Geczy committed Oct 7, 2021
1 parent a0678f3 commit 1ac8731
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 48 deletions.
35 changes: 12 additions & 23 deletions pages/api/_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface Member {
}

const draftBody = (
text: string,
prefix: string,
chat_id: number,
reply_to_message_id: number = null,
disable_notification: boolean = false,
Expand All @@ -65,7 +65,7 @@ const draftBody = (
entities: body?.message?.entities,
media: file_id,
caption_entities: body?.message?.caption_entities,
[type]: file_id || text,
[type]: file_id || prefix,
type,
};

Expand All @@ -83,28 +83,27 @@ const draftBody = (
} else if (type === 'text' && body?.message?.[type]) {
data = {
...data,
[type]: `${body?.message?.[type]}`,
[type]: `${prefix}\n\n${body.message[type]}`,
};
}

return data;
};

export const sendMsg = async (
text: string,
prefix: string,
chat_id: number,
reply_to_message_id: number = null,
disable_notification: boolean = false,
theUpdate: any = {}
) => {
console.log(theUpdate);
const body = theUpdate?.body || {};
const groupId = body?.message?.media_group_id;
const type = groupId ? 'group' : theUpdate?.type || 'text';
const apiEndpoint = telegramTypes[type] || telegramTypes.text;

let data = draftBody(
text,
prefix,
chat_id,
reply_to_message_id,
disable_notification,
Expand Down Expand Up @@ -136,31 +135,21 @@ export const sendMsg = async (
mediaGroup.length &&
mediaGroup?.[0]?.updateArchive.length
) {
console.log('Using full group', mediaGroup[0].updateArchive);
data.media = [];
mediaGroup[0].updateArchive.forEach((u) => {
data.media.push(
draftBody(text, chat_id, reply_to_message_id, disable_notification, u)
draftBody(
prefix,
chat_id,
reply_to_message_id,
disable_notification,
u
)
);
});

console.log('data.media', data.media);
}
}

console.log({
text,
chat_id,
reply_to_message_id,
disable_notification,
theUpdate,
telegramTypes,
apiEndpoint,
type,
body,
data,
});

const url = `https://api.telegram.org/bot${process.env.TELEGRAM_API_KEY}/${apiEndpoint}`;

return fetch(url, {
Expand Down
31 changes: 6 additions & 25 deletions pages/api/send.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VercelRequest, VercelResponse } from '@vercel/node';
import { connectToDatabase } from './_connectToDatabase';
import { sendMsg, StandupGroup, Member, About } from './_helpers';
import { sendMsg, StandupGroup, Member } from './_helpers';

module.exports = async (req: VercelRequest, res: VercelResponse) => {
if (
Expand All @@ -23,37 +23,18 @@ module.exports = async (req: VercelRequest, res: VercelResponse) => {
.find({ submitted: true })
.toArray();

const sentStandup = [];
const sendPromises = [];
users
.filter((g) => !!g.groups.length)

This comment has been minimized.

Copy link
@RusseII

RusseII Oct 15, 2021

Member

if you are going to have a useless variable you should just destructure

.filter(({groups}) => !!groups.length)

This comment has been minimized.

Copy link
@Geczy

Geczy Oct 16, 2021

Author Member

true

.forEach((user: Member) => {

This comment has been minimized.

Copy link
@RusseII

RusseII Oct 15, 2021

Member

I'm a bit confused by this code but are you able to do all of the separate groups in parallel with the individual messages sent to 1 group sync?

Then the time to send would be mp instead of mp*g
mp = messages per group
g = groups

user.groups.forEach((group: StandupGroup) => {
user.groups.forEach(async (group: StandupGroup) => {
const theUpdate = user.updateArchive.slice(-1)[0];
const type = theUpdate?.type;
const hasEntities =
theUpdate?.body?.message?.entities ||
theUpdate?.body?.message?.caption_entities;

if (!hasEntities && type === 'text') {
sentStandup.push(
sendMsg(
`${user.about.first_name}:`,
group.chatId,
null,
true,
theUpdate
)
);
} else {
sentStandup.push(
sendMsg(`${user.about.first_name}:`, group.chatId, null, true),
sendMsg(``, group.chatId, null, true, theUpdate)
);
}

await sendMsg(`${user.about.first_name}:`, group.chatId, null, true);
await sendMsg(``, group.chatId, null, true, theUpdate);
});
});

await Promise.all(sentStandup);
if (process.env.NODE_ENV === 'production') {
await markAllSent();
}
Expand Down

1 comment on commit 1ac8731

@vercel
Copy link

@vercel vercel bot commented on 1ac8731 Oct 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.