feat: whitelist
This commit is contained in:
@ -1,3 +1,6 @@
|
|||||||
import { setupTelegram } from "./telegram";
|
import { setupTelegram } from "./telegram";
|
||||||
|
|
||||||
setupTelegram({ bossChatId: "1502730007" });
|
setupTelegram({
|
||||||
|
bossChatId: "1502730007",
|
||||||
|
whitelist: new Set(["Johncosta27"]),
|
||||||
|
});
|
||||||
|
@ -42,9 +42,10 @@ const createChat = () => {
|
|||||||
|
|
||||||
type TelegramOptions = {
|
type TelegramOptions = {
|
||||||
bossChatId: string;
|
bossChatId: string;
|
||||||
|
whitelist: Set<string>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const setupTelegram = ({ bossChatId }: TelegramOptions) => {
|
export const setupTelegram = ({ bossChatId, whitelist }: TelegramOptions) => {
|
||||||
console.log("Setting up telegram bot");
|
console.log("Setting up telegram bot");
|
||||||
|
|
||||||
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN ?? "", {
|
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN ?? "", {
|
||||||
@ -53,16 +54,33 @@ export const setupTelegram = ({ bossChatId }: TelegramOptions) => {
|
|||||||
|
|
||||||
const chats = createChat();
|
const chats = createChat();
|
||||||
|
|
||||||
bot.on("message", async (msg) => {
|
bot.on("message", async (msg, meta) => {
|
||||||
|
console.log(msg);
|
||||||
|
|
||||||
|
if (meta.type !== "text") {
|
||||||
|
// TODO: gracefully handle messages like this.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fromUsername = msg.from?.username;
|
||||||
|
if (fromUsername == null) {
|
||||||
|
console.log("Cannot handle a message without a user.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const text = msg.text;
|
const text = msg.text;
|
||||||
if (text == null) {
|
if (text == null) {
|
||||||
console.log("Cannot handle messages without text");
|
console.log("Cannot handle messages without text.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!whitelist.has(fromUsername)) {
|
||||||
|
bot.sendMessage(msg.chat.id, "Sorry, you are not part of the whitelist.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
chats.addMessage(msg.chat.id, text);
|
chats.addMessage(msg.chat.id, text);
|
||||||
|
|
||||||
// TODO: make BOT only respond to Rio.
|
|
||||||
const messages = await getResponse(
|
const messages = await getResponse(
|
||||||
{
|
{
|
||||||
async createTasks({ response, name, date }) {
|
async createTasks({ response, name, date }) {
|
||||||
|
Reference in New Issue
Block a user