Skip to content

Commit

Permalink
radio support
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnabXD committed Feb 12, 2022
1 parent 8271437 commit e983ca1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions src/handlers/radio.ts
Original file line number Diff line number Diff line change
@@ -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'
}
);
});
2 changes: 1 addition & 1 deletion src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface QueueData {
first_name: string;
};
mp3_link: string;
provider: 'jiosaavn' | 'telegram';
provider: 'jiosaavn' | 'telegram' | 'radio';
}

class Queues {
Expand Down
10 changes: 10 additions & 0 deletions src/tgcalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TGVCCalls {
}

private async onStreamError(error: Error, chat: Chat): Promise<void> {
console.error(error);
const errorMessage = error.message || String(error);

if (errorMessage.includes('No active call')) {
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/utils/hhmmss.ts
Original file line number Diff line number Diff line change
@@ -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 = ``;
Expand Down

0 comments on commit e983ca1

Please sign in to comment.