Skip to content

Commit

Permalink
Add emoji reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
soruly committed Mar 10, 2024
1 parent 73b3b26 commit 4b256ae
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ const sendChatAction = (chat_id, action) =>
.then((e) => e.json())
.then((e) => e.result);

const setMessageReaction = (chat_id, message_id, emoji_list, is_big) =>
fetch(`${TELEGRAM_API}/bot${TELEGRAM_TOKEN}/setMessageReaction`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chat_id,
message_id,
reaction: emoji_list.map((emoji) => ({ type: "emoji", emoji })),
is_big,
}),
})
.then((e) => e.json())
.then((e) => e.result);

const sendVideo = (chat_id, video, options) =>
fetch(`${TELEGRAM_API}/bot${TELEGRAM_TOKEN}/sendVideo`, {
method: "POST",
Expand Down Expand Up @@ -312,6 +326,7 @@ const getImageFromMessage = async (message) => {
};

const privateMessageHandler = async (message) => {
setMessageReaction(message.chat.id, message.message_id, ["👌"]);
const responding_msg = message.reply_to_message ? message.reply_to_message : message;
const imageURL = await getImageFromMessage(responding_msg);
if (!imageURL) {
Expand All @@ -322,10 +337,10 @@ const privateMessageHandler = async (message) => {
}
return await sendMessage(message.chat.id, "You can Send / Forward anime screenshots to me.");
}

await sendChatAction(message.chat.id, "typing");

setMessageReaction(message.chat.id, message.message_id, ["🤔"]);
const result = await submitSearch(imageURL, responding_msg, message);
sendChatAction(message.chat.id, "typing");
setMessageReaction(message.chat.id, message.message_id, ["👍"]);

if (result.video && !messageIsSkipPreview(message)) {
const videoLink = messageIsMute(message) ? `${result.video}&mute` : result.video;
Expand All @@ -347,9 +362,7 @@ const privateMessageHandler = async (message) => {
};

const groupMessageHandler = async (message) => {
if (!messageIsMentioningBot(message)) {
return;
}
setMessageReaction(message.chat.id, message.message_id, ["👌"]);
const responding_msg = message.reply_to_message ? message.reply_to_message : message;
const imageURL = await getImageFromMessage(responding_msg);
if (!imageURL) {
Expand All @@ -366,10 +379,10 @@ const groupMessageHandler = async (message) => {
{ reply_to_message_id: message.message_id },
);
}

await sendChatAction(message.chat.id, "typing");

setMessageReaction(message.chat.id, message.message_id, ["🤔"]);
const result = await submitSearch(imageURL, responding_msg, message);
sendChatAction(message.chat.id, "typing");
setMessageReaction(message.chat.id, message.message_id, ["👍"]);

if (result.isAdult) {
await sendMessage(
Expand Down Expand Up @@ -402,13 +415,14 @@ const groupMessageHandler = async (message) => {
});
};

app.post("/", (req, res) => {
app.post("/", async (req, res) => {
const message = req.body?.message;
if (message?.chat?.type === "private") {
privateMessageHandler(message);
await privateMessageHandler(message);
} else if (message?.chat?.type === "group" || message?.chat?.type === "supergroup") {
groupMessageHandler(message);
if (messageIsMentioningBot(message)) await groupMessageHandler(message);
}
setMessageReaction(message.chat.id, message.message_id, []);
res.sendStatus(204);
});

Expand Down

0 comments on commit 4b256ae

Please sign in to comment.