Commit 1f6c7c49 authored by Aiden Buis's avatar Aiden Buis

Initial commit

Pipeline #39306303 failed with stages
in 12 minutes and 2 seconds
\ No newline at end of file
# Speech-To-Text Journal Telegram Bot
Hi There, My name is [Aiden]( :)
I am an Indie Maker and Freelance Web Developer. I recently made [Open Habits]( to help you form new habits and [LucidDreamBot](, which helps you to achieve lucid dreams. I log every step of my journey as an indie maker on [my Twitter](
After building LucidDreamBot I saw the power of using voice and I decided to make a voice-to-text journal chatbot. I tried to make this readme as clear and concise as possible and hope it inspires people to learn programming.
It is built on Telegram and the Google Cloud Text-to-Speech API. The costs of using the Google transcription API is $0,006 per 15 seconds of audio, but after signing up you receive $300 in [free credits](, which you can use for 12 months. 💸 With that you can save 208 hours of you journaling! ✍️
## Getting Started
These instructions will get your personal journaling chatbot up and running. The only thing You will need is a server to host the script and get a Google API and Telegram Bot API key.
Personally I use a [Digital Ocean]( $5/m VPS, which hosts not only this script, but all my websites. You can also use a PaaS like Heroku, but setting up a VPS yourself isn't that hard as Digital Ocean has great documentation!
Now let's dive in!
## Installing Node.js
To run the Telegram Bot locally you need Node.js which you can install [here]([](
## Pull and install the node dependencies
Pull or download this repository. After you've done that open your terminal, navigate to the folder
~ cd folder/folder/bot_folder
And type the following command to install the node dependencies. You need the NPM package manager for this, which you can install [here](
~ npm install
## Creating your chatbot
You will need to make a Telegram chatbot. You can do this by opening up [BotFather]([]( and typing
After giving it a name you will receive a API key. Make a new file in the main directory called '.env'. You can do this in the command line with, where the ~ indicates the command is run in a CLI (Command Line Interface).
~ touch .env
In this file you paste your Telegram bot API key with the following format. In the command line you can do this by typing.
~ nano .env
To exit the nano text editor press Ctrl + X. Then press 'y' to save and exit. Now the script can connect to your Telegram bot.
## Connecting the bot to the Google Cloud API
Sign up for Google Cloud [here]([]( and receive your free $300 in credits. After that go the the [Google Developers Console]
Then search for Cloud Speech API and enable it.
After that [Follow these steps]([]( to create an auth.json file. After downloading the file put it in the main directory. The script will use this to connect to the Google Cloud API and transcribe the audio.
## Running the chatbot
After that you are ready to run the bot! You can do this locally by opening up the terminal and go to the directory. To do this you can use
~ cd folder/folder/bot_folder
After that you can run the bot with
~ node speech.js
Now open up the bot you've created. You can find a link to your bot in the same message that contains your API key that BotFather send you. After opening your bot you are ready to send the voice message!
## Keeping the bot alive
With the 'speech.js' script running your bot is alive! But if you shutdown your computer it will die :'(. To keep it alive you can deploy it to your server. Then you can use a process manager like [PM2]([]( to keep it alive and running.
After deploying to your server make sure to run 'npm install' again to install the needed dependencies.
## Questions
That is it! If you have any questions feel free to reach out to me on [Twitter](
\ No newline at end of file
This diff is collapsed.
"name": "speech",
"version": "1.0.0",
"description": "",
"main": "speech.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"author": "",
"license": "ISC",
"dependencies": {
"@google-cloud/speech": "^2.1.1",
"dotenv": "^6.2.0",
"moment": "^2.22.2",
"node-telegram-bot-api": "^0.30.0",
"request": "^2.88.0"
// ******************** //
// Require packages //
// ****************** //
const fs = require('fs'),
request = require('request'),
telegramBot = require('node-telegram-bot-api'),
speech = require('@google-cloud/speech'),
moment = require('moment');
// ************************ //
// Setup Env Variables //
// ********************** //
const token = process.env.TEL_TOKEN;
// *********************** //
// Setup Telegram Bot //
// ********************* //
const bot = new telegramBot(token, {polling: true});
bot.on('voice', (msg) => {
console.log("voice message received");
var file_id = msg.voice.file_id;
var fileName = file_id;
var file = bot.getFile(file_id);
var user_id =;
file.then(function (result) {
console.log("File is ready, let's download it!");
var file_path = result.file_path;
var voice_url = `${token}/${file_path}`
var downloadFilePath = `voice/${fileName}.oga`;
download(voice_url, fileName, downloadFilePath, function(err){
var emojiList = ["🙈", "🙉", "🙊", "🐵", "🐶", "🦊", "🐱", "🦁", "🐯", "🐴", "🦄", "🐭", "🐹", "🐰", "🐻", "🐨", "🐼", "🐣", "🐤", "🐦", "🐧", "🐸", "🐲", "🐳", "🐋", "🦋"];
var emojiLogo = emojiList[Math.floor(Math.random()*emojiList.length)];
createTranscript(downloadFilePath, user_id, emojiLogo);
// **************************************** //
// Download function for the audio file //
// ************************************** //
var download = function(uri, filename, downloadFilePath, callback){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(downloadFilePath)).on('close', callback);
// *************************************** //
// Create transcript from audio file :) //
// ************************************* //
function createTranscript(filePath, user_id, emoji) {
const client = new speech.SpeechClient({
keyFilename: 'auth.json'
// Reads a local audio file and converts it to base64
const file = fs.readFileSync(filePath);
const audioBytes = file.toString('base64');
// The audio file's encoding, sample rate in hertz
const audio = {
content: audioBytes,
// The Google Cloud API supports different language codes.
// For a complete overview see:
const config = {
encoding: 'OGG_OPUS',
sampleRateHertz: 16000,
languageCode: 'en'
const request = {
audio: audio,
config: config,
// Make the transcript from the audio
.then(data => {
const response = data[0];
var transcription = response.results
.map(result => result.alternatives[0].transcript)
transcription = transcription.replace(/\bparagraph \b/g, '\n\n');
const journalDate = moment().format("dddd Do MMMM");
bot.sendMessage(user_id, `<b>${emoji} ${journalDate}</b>\n\n${transcription} `, { parse_mode: "HTML" });
// Remove the audio file after making and sending the transcript
fs.unlink(filePath, (err) => {
if (err) throw err;
console.log('The transcript was send and the audio file was deleted!');
.catch(err => {
console.error('ERROR:', err);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment