ref: replace "ctx.from.language_code" with a function to get the language code and fix ts implementation for "reply_to_message_id" (#51)

Co-authored-by: Lucas Gabriel <lucmsilva651@gmail.com>
This commit is contained in:
Giovani Finazzi 2025-05-02 21:08:13 -03:00 committed by GitHub
parent 6dce40d333
commit 87c987c16d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 193 additions and 160 deletions

View file

@ -4,135 +4,129 @@ import { isOnSpamWatch } from '../spamwatch/spamwatch';
import spamwatchMiddlewareModule from '../spamwatch/Middleware';
import axios from 'axios';
import { Context, Telegraf } from 'telegraf';
import { replyToMessageId } from '../utils/reply-to-message-id';
import { languageCode } from '../utils/language-code';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
export default (bot: Telegraf<Context>) => {
bot.command("duck", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const reply_to_message_id = replyToMessageId(ctx);
try {
const response = await axios(Resources.duckApi);
ctx.replyWithPhoto(response.data.url, {
caption: "🦆",
// reply_to_message_id works fine, using this for now to avoid errors
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
} catch (error) {
const Strings = getStrings(languageCode(ctx));
const message = Strings.duckApiErr.replace('{error}', error.message);
ctx.reply(message, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
return;
}
});
bot.command("fox", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
const reply_to_message_id = replyToMessageId(ctx);
try {
const response = await axios(Resources.foxApi);
ctx.replyWithPhoto(response.data.image, {
caption: "🦊",
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
} catch (error) {
const message = Strings.foxApiErr.replace('{error}', error.message);
ctx.reply(message, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
return;
}
});
bot.command("dog", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
const reply_to_message_id = replyToMessageId(ctx);
try {
const response = await axios(Resources.dogApi);
ctx.replyWithPhoto(response.data.message, {
caption: "🐶",
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
} catch (error) {
const message = Strings.foxApiErr.replace('{error}', error.message);
ctx.reply(message, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
return;
}
});
bot.command("cat", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
const apiUrl = `${Resources.catApi}?json=true`;
const response = await axios.get(apiUrl);
const data = response.data;
const imageUrl = `${data.url}`;
const reply_to_message_id = replyToMessageId(ctx);
try {
await ctx.replyWithPhoto(imageUrl, {
caption: `🐱`,
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
} catch (error) {
ctx.reply(Strings.catImgErr, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
};
});
bot.command(['soggy', 'soggycat'], spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const userInput = ctx.message.text.split(' ')[1];
switch (true) {
case (userInput === "2" || userInput === "thumb"):
ctx.replyWithPhoto(
Resources.soggyCat2, {
caption: Resources.soggyCat2,
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
});
break;
case (userInput === "3" || userInput === "sticker"):
ctx.replyWithSticker(
Resources.soggyCatSticker, {
// @ts-ignore
reply_to_message_id: ctx.message.message_id
});
break;
case (userInput === "4" || userInput === "alt"):
ctx.replyWithPhoto(
Resources.soggyCatAlt, {
caption: Resources.soggyCatAlt,
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
});
break;
default:
ctx.replyWithPhoto(
Resources.soggyCat, {
caption: Resources.soggyCat,
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
});
break;
};
});
const userInput = ctx.message.text.split(' ')[1];
const reply_to_message_id = replyToMessageId(ctx);
switch (true) {
case (userInput === "2" || userInput === "thumb"):
ctx.replyWithPhoto(
Resources.soggyCat2, {
caption: Resources.soggyCat2,
parse_mode: 'Markdown',
...({ reply_to_message_id })
});
break;
case (userInput === "3" || userInput === "sticker"):
ctx.replyWithSticker(
Resources.soggyCatSticker, {
// ...({ reply_to_message_id }) // to-do: fix this
});
break;
case (userInput === "4" || userInput === "alt"):
ctx.replyWithPhoto(
Resources.soggyCatAlt, {
caption: Resources.soggyCatAlt,
parse_mode: 'Markdown',
...({ reply_to_message_id })
});
break;
default:
ctx.replyWithPhoto(
Resources.soggyCat, {
caption: Resources.soggyCat,
parse_mode: 'Markdown',
...({ reply_to_message_id })
});
break;
};
});
}

View file

@ -5,10 +5,19 @@ import spamwatchMiddlewareModule from '../spamwatch/Middleware';
import axios from 'axios';
import verifyInput from '../plugins/verifyInput';
import { Context, Telegraf } from 'telegraf';
import { languageCode } from '../utils/language-code';
import { replyToMessageId } from '../utils/reply-to-message-id';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
interface Device {
brand: string;
codename: string;
model: string;
}
async function getDeviceList({ Strings, ctx }: { Strings: any, ctx: Context & { message: { text: string } } }) {
const reply_to_message_id = replyToMessageId(ctx);
try {
const response = await axios.get(Resources.codenameApi);
return response.data
@ -18,8 +27,7 @@ async function getDeviceList({ Strings, ctx }: { Strings: any, ctx: Context & {
return ctx.reply(message, {
parse_mode: "Markdown",
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
}
}
@ -27,10 +35,11 @@ async function getDeviceList({ Strings, ctx }: { Strings: any, ctx: Context & {
export default (bot: Telegraf<Context>) => {
bot.command(['codename', 'whatis'], spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const userInput = ctx.message.text.split(" ").slice(1).join(" ");
const Strings = getStrings(ctx.from?.language_code);
const { noCodename } = Strings.codenameCheck
if(verifyInput(ctx, userInput, noCodename)){
const Strings = getStrings(languageCode(ctx));
const { noCodename } = Strings.codenameCheck;
const reply_to_message_id = replyToMessageId(ctx);
if (verifyInput(ctx, userInput, noCodename)) {
return;
}
@ -40,13 +49,12 @@ export default (bot: Telegraf<Context>) => {
if (!phoneSearch) {
return ctx.reply(Strings.codenameCheck.notFound, {
parse_mode: "Markdown",
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
}
const deviceDetails = jsonRes[phoneSearch];
const device = deviceDetails.find((item) => item.brand) || deviceDetails[0];
const device = deviceDetails.find((item: Device) => item.brand) || deviceDetails[0];
const message = Strings.codenameCheck.resultMsg
.replace('{brand}', device.brand)
.replace('{codename}', userInput)
@ -55,8 +63,7 @@ export default (bot: Telegraf<Context>) => {
return ctx.reply(message, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
})
}

View file

@ -5,6 +5,7 @@ import os from 'os';
import { exec } from 'child_process';
import { error } from 'console';
import { Context, Telegraf } from 'telegraf';
import { languageCode } from '../utils/language-code';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
@ -54,7 +55,7 @@ function getSystemInfo() {
}
async function handleAdminCommand(ctx: Context & { message: { text: string } }, action: () => Promise<void>, successMessage: string, errorMessage: string) {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
const userId = ctx.from?.id;
const adminArray = process.env.botAdmins ? process.env.botAdmins.split(',').map(id => parseInt(id.trim())) : [];
if (userId && adminArray.includes(userId)) {
@ -84,7 +85,7 @@ async function handleAdminCommand(ctx: Context & { message: { text: string } },
export default (bot: Telegraf<Context>) => {
bot.command('getbotstats', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
handleAdminCommand(ctx, async () => {
const stats = getSystemInfo();
await ctx.reply(stats, {
@ -96,7 +97,7 @@ export default (bot: Telegraf<Context>) => {
});
bot.command('getbotcommit', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
handleAdminCommand(ctx, async () => {
try {
const commitHash = await getGitCommitHash();
@ -116,7 +117,7 @@ export default (bot: Telegraf<Context>) => {
});
bot.command('updatebot', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
handleAdminCommand(ctx, async () => {
try {
const result = await updateBot();
@ -136,7 +137,7 @@ export default (bot: Telegraf<Context>) => {
});
bot.command('setbotname', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
const botName = ctx.message.text.split(' ').slice(1).join(' ');
handleAdminCommand(ctx, async () => {
await ctx.telegram.setMyName(botName);
@ -144,7 +145,7 @@ export default (bot: Telegraf<Context>) => {
});
bot.command('setbotdesc', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
const botDesc = ctx.message.text.split(' ').slice(1).join(' ');
handleAdminCommand(ctx, async () => {
await ctx.telegram.setMyDescription(botDesc);
@ -152,7 +153,7 @@ export default (bot: Telegraf<Context>) => {
});
bot.command('botkickme', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
handleAdminCommand(ctx, async () => {
if (!ctx.chat) {
ctx.reply(Strings.chatNotFound, {
@ -172,7 +173,7 @@ export default (bot: Telegraf<Context>) => {
});
bot.command('getfile', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
const botFile = ctx.message.text.split(' ').slice(1).join(' ');
if (!botFile) {

View file

@ -3,11 +3,12 @@ import { getStrings } from '../plugins/checklang';
import { isOnSpamWatch } from '../spamwatch/spamwatch';
import spamwatchMiddlewareModule from '../spamwatch/Middleware';
import { Context, Telegraf } from 'telegraf';
import { languageCode } from '../utils/language-code';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
function sendRandomReply(ctx: Context & { message: { text: string } }, gifUrl: string, textKey: string) {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
const randomNumber = Math.floor(Math.random() * 100);
const shouldSendGif = randomNumber > 50;
@ -38,7 +39,7 @@ function sendRandomReply(ctx: Context & { message: { text: string } }, gifUrl: s
async function handleDiceCommand(ctx: Context & { message: { text: string } }, emoji: string, delay: number) {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
// @ts-ignore
const result = await ctx.sendDice({ emoji, reply_to_message_id: ctx.message.message_id });
@ -55,13 +56,13 @@ async function handleDiceCommand(ctx: Context & { message: { text: string } }, e
}, delay);
}
function getRandomInt(max) {
function getRandomInt(max: number) {
return Math.floor(Math.random() * (max + 1));
}
export default (bot: Telegraf<Context>) => {
bot.command('random', spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code);
const Strings = getStrings(languageCode(ctx));
const randomValue = getRandomInt(11);
const randomVStr = Strings.randomNum.replace('{number}', randomValue);

View file

@ -127,7 +127,7 @@ function formatPhone(phone: PhoneDetails) {
return `<b>\n\nName: </b><code>${formattedPhone.name}</code>\n\n${attributes}\n\n${deviceImage}\n\n${deviceUrl}`;
}
async function fetchHtml(url) {
async function fetchHtml(url: string) {
try {
const response = await axios.get(url, { headers: HEADERS });
return response.data;

View file

@ -1,6 +1,7 @@
import { getStrings } from '../plugins/checklang';
import { isOnSpamWatch } from '../spamwatch/spamwatch';
import spamwatchMiddlewareModule from '../spamwatch/Middleware';
import { languageCode } from '../utils/language-code';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
@ -14,7 +15,7 @@ interface MessageOptions {
}
async function sendHelpMessage(ctx, isEditing) {
const Strings = getStrings(ctx.from.language_code);
const Strings = getStrings(languageCode(ctx));
const botInfo = await ctx.telegram.getMe();
const helpText = Strings.botHelp
.replace(/{botName}/g, botInfo.first_name)
@ -56,7 +57,7 @@ export default (bot) => {
});
bot.command("about", spamwatchMiddleware, async (ctx) => {
const Strings = getStrings(ctx.from.language_code);
const Strings = getStrings(languageCode(ctx));
const aboutMsg = Strings.botAbout.replace(/{sourceLink}/g, `${process.env.botSource}`);
ctx.reply(aboutMsg, {
parse_mode: 'Markdown',
@ -67,7 +68,7 @@ export default (bot) => {
bot.on('callback_query', async (ctx) => {
const callbackData = ctx.callbackQuery.data;
const Strings = getStrings(ctx.from.language_code);
const Strings = getStrings(languageCode(ctx));
const options = {
parse_mode: 'Markdown',
disable_web_page_preview: true,

View file

@ -5,12 +5,14 @@ import spamwatchMiddlewareModule from '../spamwatch/Middleware';
import axios from 'axios';
import verifyInput from '../plugins/verifyInput';
import { Context, Telegraf } from 'telegraf';
import { languageCode } from '../utils/language-code';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
export default (bot: Telegraf<Context>) => {
bot.command("http", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code || 'en');
const reply_to_message_id = ctx.message.message_id;
const Strings = getStrings(languageCode(ctx));
const userInput = ctx.message.text.split(' ')[1];
const apiUrl = Resources.httpApi;
const { invalidCode } = Strings.httpCodes
@ -32,28 +34,26 @@ export default (bot: Telegraf<Context>) => {
.replace("{description}", codeInfo.description);
await ctx.reply(message, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
} else {
await ctx.reply(Strings.httpCodes.notFound, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
};
} catch (error) {
const message = Strings.httpCodes.fetchErr.replace("{error}", error);
ctx.reply(message, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
};
});
bot.command("httpcat", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code || 'en');
const Strings = getStrings(languageCode(ctx));
const reply_to_message_id = ctx.message.message_id;
const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(/\s+/g, '');
const { invalidCode } = Strings.httpCodes
@ -67,14 +67,12 @@ export default (bot: Telegraf<Context>) => {
await ctx.replyWithPhoto(apiUrl, {
caption: `🐱 ${apiUrl}`,
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
} catch (error) {
ctx.reply(Strings.catImgErr, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
}
});

View file

@ -1,18 +1,22 @@
import { getStrings } from '../plugins/checklang';
import { isOnSpamWatch } from '../spamwatch/spamwatch';
import spamwatchMiddlewareModule from '../spamwatch/Middleware';
import { Context, Telegraf } from 'telegraf';
import { replyToMessageId } from '../utils/reply-to-message-id';
import { languageCode } from '../utils/language-code';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
export default (bot: any) => {
bot.start(spamwatchMiddleware, async (ctx: any) => {
const Strings = getStrings(ctx.from.language_code);
export default (bot: Telegraf<Context>) => {
bot.start(spamwatchMiddleware, async (ctx: Context) => {
const Strings = getStrings(languageCode(ctx));
const botInfo = await ctx.telegram.getMe();
const reply_to_message_id = replyToMessageId(ctx)
const startMsg = Strings.botWelcome.replace(/{botName}/g, botInfo.first_name);
ctx.reply(startMsg, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
});

View file

@ -5,6 +5,9 @@ import path from 'path';
import { getStrings } from '../plugins/checklang';
import { isOnSpamWatch } from '../spamwatch/spamwatch';
import spamwatchMiddlewareModule from '../spamwatch/Middleware';
import { languageCode } from '../utils/language-code';
import { Context, Telegraf } from 'telegraf';
import { replyToMessageId } from '../utils/reply-to-message-id';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
@ -46,15 +49,16 @@ async function downloadModule(moduleId: string): Promise<ModuleResult | null> {
}
}
export default (bot) => {
export default (bot: Telegraf<Context>) => {
bot.command(['modarchive', 'tma'], spamwatchMiddleware, async (ctx) => {
const Strings = getStrings(ctx.from.language_code);
const moduleId = ctx.message.text.split(' ')[1];
const Strings = getStrings(languageCode(ctx));
const reply_to_message_id = replyToMessageId(ctx);
const moduleId = ctx.message?.text.split(' ')[1];
if (Number.isNaN(moduleId) || null) {
return ctx.reply(Strings.maInvalidModule, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
}
@ -65,14 +69,14 @@ export default (bot) => {
await ctx.replyWithDocument({ source: filePath }, {
caption: fileName,
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
fs.unlinkSync(filePath);
} else {
ctx.reply(Strings.maDownloadError, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
}
});

View file

@ -5,6 +5,8 @@ import spamwatchMiddlewareModule from '../spamwatch/Middleware';
import axios from 'axios';
import verifyInput from '../plugins/verifyInput';
import { Telegraf, Context } from 'telegraf';
import { languageCode } from '../utils/language-code';
import { replyToMessageId } from '../utils/reply-to-message-id';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
@ -47,24 +49,24 @@ interface Comic {
editor: string;
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
function capitalizeFirstLetter(letter: string) {
return letter.charAt(0).toUpperCase() + letter.slice(1);
}
export default (bot: Telegraf<Context>) => {
bot.command("mlp", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code || 'en');
const Strings = getStrings(languageCode(ctx));
const reply_to_message_id = replyToMessageId(ctx);
ctx.reply(Strings.ponyApi.helpDesc, {
parse_mode: 'Markdown',
// @ts-ignore
disable_web_page_preview: true,
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id, disable_web_page_preview: true })
});
});
bot.command("mlpchar", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code || 'en');
const reply_to_message_id = replyToMessageId(ctx);
const Strings = getStrings(languageCode(ctx) || 'en');
const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(" ", "+");
const { noCharName } = Strings.ponyApi
@ -118,30 +120,27 @@ export default (bot: Telegraf<Context>) => {
ctx.replyWithPhoto(charactersArray[0].image[0], {
caption: `${result}`,
parse_mode: 'Markdown',
// @ts-ignore
disable_web_page_preview: true,
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id, disable_web_page_preview: true })
});
} else {
ctx.reply(Strings.ponyApi.noCharFound, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
};
} catch (error) {
const message = Strings.ponyApi.apiErr.replace('{error}', error.message);
ctx.reply(message, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
};
});
bot.command("mlpep", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code || 'en');
const Strings = getStrings(languageCode(ctx) || 'en');
const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(" ", "+");
const reply_to_message_id = replyToMessageId(ctx);
const { noEpisodeNum } = Strings.ponyApi
@ -156,7 +155,7 @@ export default (bot: Telegraf<Context>) => {
const episodeArray: Episode[] = [];
if (Array.isArray(response.data.data)) {
response.data.data.forEach(episode => {
response.data.data.forEach((episode: Episode) => {
episodeArray.push({
id: episode.id,
name: episode.name,
@ -189,30 +188,29 @@ export default (bot: Telegraf<Context>) => {
ctx.replyWithPhoto(episodeArray[0].image, {
caption: `${result}`,
parse_mode: 'Markdown',
// @ts-ignore
disable_web_page_preview: true,
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id, disable_web_page_preview: true })
});
} else {
ctx.reply(Strings.ponyApi.noEpisodeFound, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
};
} catch (error) {
const message = Strings.ponyApi.apiErr.replace('{error}', error.message);
ctx.reply(message, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
};
});
bot.command("mlpcomic", spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code || 'en');
const Strings = getStrings(languageCode(ctx) || 'en');
const userInput = ctx.message.text.split(' ').slice(1).join(' ').replace(" ", "+");
const reply_to_message_id = replyToMessageId(ctx);
const { noComicName } = Strings.ponyApi
@ -265,23 +263,21 @@ export default (bot: Telegraf<Context>) => {
ctx.replyWithPhoto(comicArray[0].image, {
caption: `${result}`,
parse_mode: 'Markdown',
// @ts-ignore
disable_web_page_preview: true,
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id, disable_web_page_preview: true })
});
} else {
ctx.reply(Strings.ponyApi.noComicFound, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
};
} catch (error) {
const message = Strings.ponyApi.apiErr.replace('{error}', error.message);
ctx.reply(message, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
};
});

View file

@ -4,13 +4,16 @@ import { isOnSpamWatch } from '../spamwatch/spamwatch';
import spamwatchMiddlewareModule from '../spamwatch/Middleware';
import axios from 'axios';
import { Telegraf, Context } from 'telegraf';
import { languageCode } from '../utils/language-code';
import { replyToMessageId } from '../utils/reply-to-message-id';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
export default (bot: Telegraf<Context>) => {
// TODO: this would greatly benefit from a loading message
bot.command(["rpony", "randompony", "mlpart"], spamwatchMiddleware, async (ctx: Context & { message: { text: string } }) => {
const Strings = getStrings(ctx.from?.language_code || 'en');
const Strings = getStrings(languageCode(ctx));
const reply_to_message_id = replyToMessageId(ctx);
try {
const response = await axios(Resources.randomPonyApi);
let tags: string[] = [];
@ -26,15 +29,13 @@ export default (bot: Telegraf<Context>) => {
ctx.replyWithPhoto(response.data.pony.representations.full, {
caption: `${response.data.pony.sourceURL}\n\n${tags.length > 0 ? tags.join(', ') : ''}`,
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
} catch (error) {
const message = Strings.ponyApi.apiErr.replace('{error}', error.message);
ctx.reply(message, {
parse_mode: 'Markdown',
// @ts-ignore
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
return;
}

View file

@ -8,6 +8,7 @@ import { getStrings } from '../plugins/checklang';
import { isOnSpamWatch } from '../spamwatch/spamwatch';
import spamwatchMiddlewareModule from '../spamwatch/Middleware';
import verifyInput from '../plugins/verifyInput';
import { Context, Telegraf } from 'telegraf';
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
@ -33,8 +34,9 @@ function getLocaleUnit(countryCode: string) {
}
}
export default (bot) => {
export default (bot: Telegraf<Context>) => {
bot.command(['clima', 'weather'], spamwatchMiddleware, async (ctx) => {
const reply_to_message_id = ctx.message.message_id;
const userLang = ctx.from.language_code || "en-US";
const Strings = getStrings(userLang);
const userInput = ctx.message.text.split(' ').slice(1).join(' ');
@ -50,7 +52,7 @@ export default (bot) => {
if (!apiKey || apiKey === "InsertYourWeatherDotComApiKeyHere") {
return ctx.reply(Strings.weatherStatus.apiKeyErr, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
}
@ -69,7 +71,7 @@ export default (bot) => {
if (!locationData || !locationData.address) {
return ctx.reply(Strings.weatherStatus.invalidLocation, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
}
@ -106,13 +108,13 @@ export default (bot) => {
ctx.reply(weatherMessage, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
} catch (error) {
const message = Strings.weatherStatus.apiErr.replace('{error}', error.message);
ctx.reply(message, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
}
});

View file

@ -1,5 +1,7 @@
/*
import axios from "axios";
import { Context, Telegraf } from "telegraf";
import { replyToMessageId } from "../utils/reply-to-message-id";
function capitalizeFirstLetter(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1);
@ -23,7 +25,7 @@ function mediaWikiToMarkdown(input: string) {
return input;
}
export default (bot) => {
export default (bot: Telegraf<Context>) => {
bot.command("wiki", async (ctx) => {
const userInput = capitalizeFirstLetter(ctx.message.text.split(' ')[1]);
const apiUrl = `https://en.wikipedia.org/w/index.php?title=${userInput}&action=raw`;
@ -31,8 +33,9 @@ export default (bot) => {
const convertedResponse = response.data.replace(/<\/?div>/g, "").replace(/{{Infobox.*?}}/s, "");
const result = mediaWikiToMarkdown(convertedResponse).slice(0, 2048);
const reply_to_message_id = replyToMessageId(ctx);
ctx.reply(result, { parse_mode: 'Markdown', disable_web_page_preview: true, reply_to_message_id: ctx.message.message_id });
ctx.reply(result, { parse_mode: 'Markdown', ...({ reply_to_message_id, disable_web_page_preview: true }) });
});
};
*/

View file

@ -7,7 +7,10 @@ const languageFiles = {
'en-gb': '../locales/english.json'
};
function getStrings(languageCode: string) {
function getStrings(languageCode?: string) {
if (!languageCode) {
return require(languageFiles['en']);
}
const filePath: string = languageFiles[languageCode] || languageFiles['en'];
try {
return require(filePath);

View file

@ -1,8 +1,12 @@
export default function verifyInput(ctx: any, userInput: string, message: string, verifyNaN = false) {
if (!userInput || (verifyNaN && isNaN(Number(userInput)))) { // not sure why isNaN is used here, but the input should be a number
import { Context } from "telegraf";
import { replyToMessageId } from "../utils/reply-to-message-id";
export default function verifyInput(ctx: Context, userInput: string, message: string, verifyNaN = false) {
const reply_to_message_id = replyToMessageId(ctx);
if (!userInput || (verifyNaN && isNaN(Number(userInput)))) {
ctx.reply(message, {
parse_mode: "Markdown",
reply_to_message_id: ctx.message.message_id
...({ reply_to_message_id })
});
return true;
}

View file

@ -0,0 +1,9 @@
import { Context } from "telegraf";
export const languageCode = (ctx: Context) => {
if(ctx.from) {
return ctx.from.language_code
} else {
return 'en'
}
}

View file

@ -0,0 +1,5 @@
import { Context } from "telegraf"
export const replyToMessageId = (ctx: Context) => {
return ctx.message?.message_id
}