-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcabca9
commit c0c501d
Showing
5 changed files
with
120 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,13 @@ | ||
import TelegramBot, { InlineQueryResultCachedVoice } from 'node-telegram-bot-api'; | ||
import fs from 'fs'; | ||
import { LocalVoiceMap, TelegramVoiceMap } from './types'; | ||
import lodash from 'lodash'; | ||
const { isEqual } = lodash; | ||
import dotenv from 'dotenv'; | ||
import { BotWrapper } from './bot'; | ||
import { VoicesLoader } from './voices-loader'; | ||
|
||
dotenv.config(); | ||
const token = process.env.TELEGRAM_BOT_TOKEN as string; | ||
|
||
const localVoicesFilepath = './assets/local_voice_map.json'; | ||
const localVoicesFile = fs.readFileSync(localVoicesFilepath, 'utf-8'); | ||
const files: LocalVoiceMap = JSON.parse(localVoicesFile); | ||
|
||
// Create a bot that uses 'polling' to fetch new updates | ||
const bot = new TelegramBot(token, { polling: true }); | ||
|
||
const voiceMapFilepath = './assets/telegram_voice_map.json'; | ||
const voiceMapFile = fs.readFileSync(voiceMapFilepath, 'utf-8'); | ||
const voiceMap: TelegramVoiceMap = JSON.parse(voiceMapFile); | ||
const latestVoiceMap: TelegramVoiceMap = JSON.parse(voiceMapFile); | ||
|
||
for (const file of files.voices) { | ||
const voice = voiceMap.voices.find(v => v.id === file.id); | ||
|
||
// Upload if voice doesn't exist | ||
if (!voice) { | ||
const data = fs.createReadStream(file.path); | ||
const msg = await bot.sendVoice(459393176, data); // send to my chat id | ||
|
||
if (!msg.voice) { | ||
throw Error("Did not get an expected file_id from Telegram"); | ||
} | ||
|
||
latestVoiceMap.voices.push({ | ||
id: file.id, | ||
file_id: msg.voice.file_id, | ||
title: file.title, | ||
}); | ||
} | ||
} | ||
if (!isEqual(voiceMap, latestVoiceMap)) { | ||
fs.writeFileSync(voiceMapFilepath, JSON.stringify(latestVoiceMap), { encoding: 'utf-8' }); | ||
} | ||
|
||
latestVoiceMap.voices = latestVoiceMap.voices.sort((a, b) => { | ||
if (a.title > b.title) { | ||
return 1; | ||
} else if (a.title < b.title) { | ||
return -1; | ||
} | ||
return 0; | ||
}) | ||
|
||
bot.on('inline_query', async query => { | ||
const inlineMsg = query.query.toLowerCase(); | ||
let results = latestVoiceMap.voices; | ||
|
||
if (inlineMsg.length > 1) { | ||
results = latestVoiceMap.voices.filter(v => v.title.toLowerCase().includes(inlineMsg)); | ||
} | ||
|
||
if (results.length > 50) { | ||
results = results.slice(0, 49); | ||
} | ||
|
||
const voices = results.map(v => ({ | ||
id: v.id, | ||
title: v.title, | ||
voice_file_id: v.file_id, | ||
type: 'voice' | ||
}) as InlineQueryResultCachedVoice); | ||
const botWrapper = new BotWrapper(); | ||
const bot = botWrapper.init(); | ||
const loader = new VoicesLoader(bot); | ||
const voiceMap = loader.load(); | ||
|
||
bot.answerInlineQuery(query.id, voices); | ||
}); | ||
botWrapper.bind(voiceMap); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import TelegramBot, { InlineQueryResultCachedVoice } from "node-telegram-bot-api"; | ||
import { TelegramVoiceMap } from "./types"; | ||
|
||
export class BotWrapper { | ||
|
||
private bot?: TelegramBot; | ||
private readonly token: string; | ||
|
||
constructor() { | ||
this.token = process.env.TELEGRAM_BOT_TOKEN as string; | ||
} | ||
|
||
init(): TelegramBot { | ||
// Create a bot that uses 'polling' to fetch new updates | ||
this.bot = new TelegramBot(this.token, { polling: true }); | ||
return this.bot; | ||
} | ||
|
||
async bind(voiceMap: Promise<TelegramVoiceMap>) { | ||
const map = await voiceMap; | ||
this.bot?.on('inline_query', async query => { | ||
const inlineMsg = query.query.toLowerCase(); | ||
let results = map.voices; | ||
|
||
if (inlineMsg.length > 1) { | ||
results = map.voices.filter(v => v.title.toLowerCase().includes(inlineMsg)); | ||
} | ||
|
||
if (results.length > 50) { | ||
results = results.slice(0, 49); | ||
} | ||
|
||
const voices = results.map(v => ({ | ||
id: v.id, | ||
title: v.title, | ||
voice_file_id: v.file_id, | ||
type: 'voice' | ||
}) as InlineQueryResultCachedVoice); | ||
|
||
this.bot?.answerInlineQuery(query.id, voices); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { LocalVoiceMap, TelegramVoiceMap } from "./types"; | ||
import fs from 'fs'; | ||
import TelegramBot from "node-telegram-bot-api"; | ||
import { isEqual } from "lodash"; | ||
|
||
export class VoicesLoader { | ||
private readonly localVoicesFilepath = './assets/local_voice_map.json'; | ||
private readonly voiceMapFilepath = './assets/telegram_voice_map.json'; | ||
private readonly myChatId = 459393176; | ||
|
||
private bot: TelegramBot; | ||
|
||
constructor(bot: TelegramBot) { | ||
this.bot = bot; | ||
} | ||
|
||
async load(): Promise<TelegramVoiceMap> { | ||
const localVoicesFile = fs.readFileSync(this.localVoicesFilepath, 'utf-8'); | ||
const files: LocalVoiceMap = JSON.parse(localVoicesFile); | ||
|
||
const voiceMapFile = fs.readFileSync(this.voiceMapFilepath, 'utf-8'); | ||
const voiceMap: TelegramVoiceMap = JSON.parse(voiceMapFile); | ||
const latestVoiceMap: TelegramVoiceMap = JSON.parse(voiceMapFile); | ||
|
||
await this.uploadNewVoices(files, voiceMap, latestVoiceMap); | ||
|
||
this.sortVoices(latestVoiceMap); | ||
|
||
return latestVoiceMap; | ||
} | ||
|
||
private async uploadNewVoices(files: LocalVoiceMap, voiceMap: TelegramVoiceMap, newVoiceMap: TelegramVoiceMap) { | ||
for (const file of files.voices) { | ||
const voice = voiceMap.voices.find(v => v.id === file.id); | ||
|
||
// Upload if voice doesn't exist | ||
if (!voice) { | ||
const data = fs.createReadStream(file.path); | ||
const msg = await this.bot.sendVoice(this.myChatId, data); // send to my chat id | ||
|
||
if (!msg.voice) { | ||
throw Error("Did not get an expected file_id from Telegram"); | ||
} | ||
|
||
newVoiceMap.voices.push({ | ||
id: file.id, | ||
file_id: msg.voice.file_id, | ||
title: file.title, | ||
}); | ||
} | ||
} | ||
|
||
// Persist new voice map | ||
if (!isEqual(voiceMap, newVoiceMap)) { | ||
fs.writeFileSync(this.voiceMapFilepath, JSON.stringify(newVoiceMap), { encoding: 'utf-8' }); | ||
} | ||
} | ||
|
||
private sortVoices(latestVoiceMap: TelegramVoiceMap) { | ||
latestVoiceMap.voices = latestVoiceMap.voices.sort((a, b) => { | ||
if (a.title > b.title) { | ||
return 1; | ||
} else if (a.title < b.title) { | ||
return -1; | ||
} | ||
return 0; | ||
}); | ||
} | ||
} |