Skip to content

Commit

Permalink
Telegram support
Browse files Browse the repository at this point in the history
  • Loading branch information
dashroshan committed Sep 29, 2023
1 parent be43142 commit 434e265
Show file tree
Hide file tree
Showing 4 changed files with 2,272 additions and 12 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ const contestsScrapingLoop = require('./loops/contests scraping');
const contestsMessageLoop = require('./loops/contests message');
const problemMessageLoop = require('./loops/problem message');
const joiningMessage = require('./utility/joining message');
const { tokenTest, tokenProd, mongourlTest, mongourlProd, isProduction } = require('./config.json');
const { tokenTest, tokenProd, mongourlTest, mongourlProd, isProduction, tokenTelegram } = require('./config.json');
const { Client, Collection, GatewayIntentBits, EmbedBuilder, PermissionFlagsBits, ActivityType } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const TelegramBot = require('node-telegram-bot-api');

client.telegramBot = new TelegramBot(tokenTelegram, { polling: true });

// Initilize mongoDB connection
const mongoose = require('mongoose');
Expand Down
13 changes: 12 additions & 1 deletion loops/contests message.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { EmbedBuilder } = require('discord.js');
const { channelTelegram } = require("../config.json")

// Data about the different supported platforms
const platforms = {
Expand All @@ -18,14 +19,24 @@ async function notify(client) {

// Format the information about the contests starting soon for the embed body
let respStr = "";
let telegramStr = "🔥 Coding contest in an hour 🔥";
let telegramBtns = [];
for (let i = 0; i < contests.length; i++) {
let contestData = contests[i];
let hours = Math.floor(contestData['duration'] / 3600);
let mins = Math.floor((contestData['duration'] / 60) % 60);
respStr += `**[${contestData['name']}](${contestData['url']})**\n:dart: **Platform:** ${platforms[contestData['platform']]}\n:calendar: **Start:** <t:${contestData['start']}:R>\n:stopwatch: **Duration:** ${hours} ${hours === 1 ? 'hour' : 'hours'}${mins === 0 ? '' : (' and ' + mins + ' minutes')}`;
let time = `${hours} ${hours === 1 ? 'hour' : 'hours'}${mins === 0 ? '' : (' and ' + mins + ' minutes')}`;
telegramStr += `\n\n❇️ ${contestData['name']}\n🎲 ${platforms[contestData['platform']]}\n⏰ ${time}`;
telegramBtns.push({ text: `${platforms[contestData['platform']]} Contest`, url: contestData['url'] });
respStr += `**[${contestData['name']}](${contestData['url']})**\n:dart: **Platform:** ${platforms[contestData['platform']]}\n:calendar: **Start:** <t:${contestData['start']}:R>\n:stopwatch: **Duration:** ${time}`;
if (i !== contests.length - 1) respStr += "\n\n";
}

// Send message on telegram channel
client.telegramBot.sendMessage(channelTelegram, telegramStr, {
parse_mode: 'Markdown', reply_markup: { inline_keyboard: [telegramBtns] }
});

// Create the embed to be sent to all channels
const embed = new EmbedBuilder()
.setColor(0x1089DF)
Expand Down
Loading

0 comments on commit 434e265

Please sign in to comment.