diff --git a/src/handlers/index.ts b/src/handlers/index.ts index 90a69cf..f339f8b 100644 --- a/src/handlers/index.ts +++ b/src/handlers/index.ts @@ -13,11 +13,13 @@ import jiosaavn from './jiosaavn'; import leave from './leave'; import play from './play'; import queue from './queue'; +import radio from './radio'; import start from './start'; export const InitHandlers = (): void => { bot.use(start); bot.use(jiosaavn); + bot.use(radio); bot.use(play); bot.use(controls); bot.use(queue); diff --git a/src/handlers/radio.ts b/src/handlers/radio.ts new file mode 100644 index 0000000..000678b --- /dev/null +++ b/src/handlers/radio.ts @@ -0,0 +1,47 @@ +/** + * Copyright 2021 Arnab Paryali and the Contributors - https://github.com/ArnabXD/TGVCBot/graphs/contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + */ + +import { Composer } from 'grammy'; +import { commandExtractor } from '../utils'; +import { tgcalls } from '../tgcalls'; +import env from '../env'; + +const composer = new Composer(); + +export default composer; + +composer.command(['radio', 'stream'], async (ctx) => { + await ctx.api.sendChatAction(ctx.chat.id, 'record_voice'); + + if (ctx.chat.type === 'private') + return await ctx.reply('This Command works on Group Only'); + + let { args: keyword } = commandExtractor(ctx.message!.text); + if (!keyword) return await ctx.reply('Please provide a radio/stream link'); + + if (!keyword.startsWith('http')) { + return await ctx.reply('Invalid'); + } + + await tgcalls.streamOrQueue( + { id: ctx.chat.id, name: ctx.chat.title }, + { + link: keyword, + title: 'Radio', + image: env.THUMBNAIL, + artist: '...', + duration: '500', + requestedBy: { + id: ctx.from!.id, + first_name: ctx.from!.first_name + }, + mp3_link: keyword, + provider: 'radio' + } + ); +}); diff --git a/src/queue.ts b/src/queue.ts index 11dec05..46524b3 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -17,7 +17,7 @@ export interface QueueData { first_name: string; }; mp3_link: string; - provider: 'jiosaavn' | 'telegram'; + provider: 'jiosaavn' | 'telegram' | 'radio'; } class Queues { diff --git a/src/tgcalls.ts b/src/tgcalls.ts index fdfdab4..8cd9db2 100644 --- a/src/tgcalls.ts +++ b/src/tgcalls.ts @@ -54,6 +54,7 @@ class TGVCCalls { } private async onStreamError(error: Error, chat: Chat): Promise { + console.error(error); const errorMessage = error.message || String(error); if (errorMessage.includes('No active call')) { @@ -171,6 +172,15 @@ class TGVCCalls { }); await sendPlayingMessage(chat, { ...data, image: poster }); } + + if (data.provider === 'radio') { + let [readable] = await ffmpeg(data.mp3_link); + await tgcalls.stream({ + audio: readable, + audioOptions: streamParams + }); + await sendPlayingMessage(chat, data); + } } } diff --git a/src/utils/hhmmss.ts b/src/utils/hhmmss.ts index f05aff3..9410446 100644 --- a/src/utils/hhmmss.ts +++ b/src/utils/hhmmss.ts @@ -1,4 +1,7 @@ export const hhmmss = (duration: string): string => { + if (!/^d+$/.test(duration)) { + return '∞'; + } let sec = parseInt(duration, 10); let hms = new Date(1000 * sec).toISOString().substr(11, 8).split(':'); let str = ``;