Something went wrong. Try again.
A scrap Telegram Bot built as a part of my self-learning and self-experimenting adventures on anything and everything. The bot is built for…
Something went wrong. Try again.
my-cipher-bot index.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374var PORT = process.env.PORT || 3000;
require('dotenv').config()process.env.NTBA_FIX_319 = 1;
var request = require('request');const TelegramBot = require('node-telegram-bot-api');
// Bot configconst token = process.env.BOT_TOKEN;const bot = new TelegramBot(token, { polling: true});
// Reply to /startbot.onText(/\/start/, (msg, match) => { const start = match.input.split(' ')[1];
// var start = match[1]; // console.log(start);
var chatId = msg.chat.id; if (start === undefined) { bot.sendMessage( chatId, 'Welcome, " ' + msg.chat.first_name + ' ' + msg.chat.last_name + ' " to " My Cipher Bot ",\nThe personalized Telegram Bot for self-learning and ' + '\nself-experimenting adventures on anything and everything, \nfor own creator " Mr. Arjun Krishna ".\n🙂' ); // return;
setTimeout(() => { bot.sendMessage( chatId, '😂 Sorry for being so official in words at the first time... 😂' ); }, 500); // return;
setTimeout(() => { bot.sendMessage( chatId, 'Hello ' + msg.chat.first_name + ',' + '\nMyself Cipher Bot. \nI am the personal telegram assistant of " Mr. Decoded_Cipher ".' + '\nSorry, " Mr. Arjun Krishna ".' + '\n\nI was built for serving purposes, that my creator finds it boring, but useful and is essential.' + ' He also let me serve his friends and collegues, who may share his same feeling.' + '\n\nYou can access my features by utilizing the commands listed, that precede a "/" symbol.' + '\nWhat are you waiting for... \nLet us dig in...\n🙂' ); }, 1000); // return;
setTimeout(() => { bot.sendMessage( chatId, 'Let these examples guide you to use the commands effectively:' + '\n\n- /hellobot Hai - Waiting for your message\n("Hai" is any message)' + '\n- /movie Avatar - For Movie details\n("Avatar" is any movie name)' + '\n- /article Bitcoin - For blogs and articles\n("Bitcoin" is any keyword)' + '\n- /news Covid - For daily news updations\n("Covid" is any news keyword)' + '\n- /weather Kottayam - For weather updation\n("Kottayam" is any main location)' ); }, 2000); // return;
setTimeout(() => { bot.sendMessage( chatId, 'Hey, What are you waiting for... \n Just try something...' ); }, 5000); return; }});
// Reply to /hellobotbot.onText(/\/hellobot (.+)/, (msg, match) => { bot.sendMessage(msg.chat.id, 'Ahoy Pirate! Its me, your Cipher Bot. Thanks for your call. But, dont disturb me for an hour or so. Let me have a nap. Im feeling so sleepy. Bye.');});
// Reply to /moviebot.onText(/\/movie (.+)/, (msg, match) => { var movie = match[1]; var chatId = msg.chat.id; request(`http://www.omdbapi.com/?apikey=82043cb7&t=${movie}`, function (error, response, body) { if (!error && response.statusCode == 200) { bot.sendMessage(chatId, '_Looking for _' + movie + '...', { parse_mode: 'Markdown' })
// bot.sendMessage(chatId, 'Result:\n' + body)
.then(function (msg) { var res = JSON.parse(body);
// console.log(res);
bot.sendPhoto(chatId, res.Poster, { caption: 'Result: \nTitle: ' + res.Title + '\nYear: ' + res.Year + '\nRated: ' + res.Rated + '\nReleased: ' + res.Released + '\nRuntime: ' + res.Runtime + '\nGenre: ' + res.Genre + '\nLanguage: ' + res.Language + '\nDirector: ' + res.Director + '\nBoxOffice: ' + res.BoxOffice + '\nProduction: ' + res.Production + '\nAwards: ' + res.Awards }) }) } });});
// Reply to /weatherbot.onText(/\/weather (.+)/, (msg, match) => { var weather = match[1]; var chatId = msg.chat.id;
request(`https://api.openweathermap.org/data/2.5/weather?appid=${process.env.WEATHER_TOKEN}&q=${weather}`, function (error, response, body) { if (!error && response.statusCode == 200) { bot.sendMessage(chatId, `_Fetching weather details from ${weather}..._`, { parse_mode: 'Markdown' }) .then(function (msg) { var res = JSON.parse(body);
// console.log(res);
var temperature = Math.floor(res.main.temp - 273.15); var sky, clouds = res.clouds.all;
if (clouds >= 0 && clouds <= 10) sky = 'Clear Sky'; else if (clouds >= 11 & clouds <= 25) sky = 'Few Clouds'; else if (clouds >= 26 & clouds <= 50) sky = 'Scattered Clouds'; else if (clouds >= 51 & clouds <= 84) sky = 'Broken Clouds'; else sky = 'Overcast Clouds';
var sec = res.dt; var date = new Date(sec * 1000); var presentTime = date.toLocaleTimeString(); // console.log(date);
var sec = res.sys.sunrise; var date = new Date(sec * 1000); var sunrise = date.toLocaleTimeString(); // console.log(sunrise);
var sec = res.sys.sunset; var date = new Date(sec * 1000); var sunset = date.toLocaleTimeString(); // console.log(sunset);
bot.sendMessage(chatId, // bot.sendPhoto(chatId, icon_url,{caption: 'Result : \nCity : ' + res.name + ', ' + res.sys.country + // '\nCoordinates:' + res.name + ', ' + res.sys.country + '\n\nTime (IST) : ' + presentTime + '\nAtmosphere : ' + res.weather[0].main + '\nCloudliness : ' + sky + '\nTemperature : ' + temperature + '°C' + '\nHumidity : ' + res.main.humidity + ' %' + '\nPressure : ' + res.main.pressure + ' hPa' + '\nWind : ' + res.wind.speed + ' m/s ' + '| Angle : ' + res.wind.deg + '°' + '\n\nSunrise : ' + sunrise + '\nSunset : ' + sunset + '\n\nLatitude : ' + res.coord.lat + '\nLongitude : ' + res.coord.lon // } ); }) } })})
// Reply to /newsbot.onText(/\/news (.+)/, (msg, match) => { var news = match[1]; var chatId = msg.chat.id;
request(`http://newsapi.org/v2/top-headlines?q=${news}&apiKey=${process.env.NEWS_TOKEN}&country=in`, function (error, response, body) { if (!error && response.statusCode == 200) { bot.sendMessage(chatId, `_Fetching today's headlines on ${news}..._`, { parse_mode: 'Markdown' }) .then(function (msg) { var res = JSON.parse(body);
// console.log(res);
for (var result = 1; result <= res.totalResults; result++) {
var d = new Date(res.articles[result - 1].publishedAt); var presentTime = d.toLocaleTimeString(); var presentDate = d.toLocaleDateString();
// console.log(presentTime); // console.log(presentDate); // console.log(d);
bot.sendMessage(chatId, 'Source : ' + res.articles[result - 1].source.name + '\n' + 'Author: ' + res.articles[result - 1].author + '\n' + 'Published At : ' + presentDate + ', ' + presentTime + '\n\n' + res.articles[result - 1].title + '\n\n' + res.articles[result - 1].description + '\n\n' + res.articles[result - 1].content + '\n\n' + 'Link :\n' + res.articles[result - 1].url ) } }) } })})
// Reply to /articlesbot.onText(/\/articles (.+)/, (msg, match) => { var article = match[1]; var chatId = msg.chat.id;
request(`http://newsapi.org/v2/everything?q=${article}&apiKey=${process.env.NEWS_TOKEN}`, function (error, response, body) { if (!error && response.statusCode == 200) { bot.sendMessage(chatId, `_Fetching recent articles on ${article}..._`, { parse_mode: 'Markdown' }) .then(function (msg) { var res = JSON.parse(body);
// console.log(res);
for (var result = 1; result <= res.totalResults; result++) {
var d = new Date(res.articles[result - 1].publishedAt); var presentTime = d.toLocaleTimeString(); var presentDate = d.toLocaleDateString();
bot.sendMessage(chatId, 'Source : ' + res.articles[result - 1].source.name + '\n' + 'Author: ' + res.articles[result - 1].author + '\n' + 'Published At : ' + presentDate + ', ' + presentTime + '\n\n' + res.articles[result - 1].title + '\n\n' + res.articles[result - 1].description + '\n\n' + res.articles[result - 1].content + '\n\n' + 'Link :\n' + res.articles[result - 1].url ) } }) } })})
// Reply to /search
// bot.onText(/\/search (.+)/, (msg, match) => {// var search = match[1];// var chatId = msg.chat.id;
// console.log(search);
// request(`https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&titles=${search}`, function (error, response, body) {// if (!error && response.statusCode == 200) {// bot.sendMessage(chatId, `_Searching details of ${search} on Wikipedia..._`, {// parse_mode: 'Markdown'// })// .then(function (msg) {// var res = JSON.parse(body);// bot.sendMessage(chatId, body.query.pages);
// console.log(res);// console.log(res.query.pages);// })// }// })// })
// Reply to /covid
// bot.onText(/\/covid/, (msg, match) => {// const corona = match.input.split(' ')[1];// var chatId = msg.chat.id;// if (corona === undefined) {
// bot.sendMessage(msg.chat.id, 'Got it ' + msg.chat.first_name + ', What you want to know...?', {// reply_markup: {// inline_keyboard: [// [{// text: 'Global Status',// callback_data: 'global'// }, {// text: 'National Status',// callback_data: 'national'// }]// ]// }// });
// }// });
// bot.on("callback_query", (callback_Query) => {// const message = callback_Query.message;// if (callback_Query.data == 'global') {// // console.log('Global');
// request(`https://thevirustracker.com/free-api?global=stats`, function (error, response, body) {// if (!error && response.statusCode == 200) {// bot.sendMessage(message.chat.id, '_Looking for _' + '_covid updates around the globe_' + '...', {// parse_mode: 'Markdown'// })// .then(function (msg) {// var res = JSON.parse(body);// // console.log(res)// // console.log(res.results[0].total_new_cases_today)
// bot.sendMessage(message.chat.id,// 'Result:\nTotal Affected Countries : ' + res.results[0].total_affected_countries +// '\n\nNew Cases Today : ' + res.results[0].total_new_cases_today +// '\nNew Deaths Today : ' + res.results[0].total_new_deaths_today +// '\n\nTotal Active Cases : ' + res.results[0].total_active_cases +// '\nTotal Serious Cases : ' + res.results[0].total_serious_cases +// '\nTotal Deaths : ' + res.results[0].total_cases// )
// })
// }// });
// } else {
// request(`https://thevirustracker.com/free-api?countryTimeline=IN`, function (error, response, body) {// if (!error && response.statusCode == 200) {// bot.sendMessage(message.chat.id, '_Looking for _' + '_covid updates around the globe_' + '...', {// parse_mode: 'Markdown'// })// .then(function (msg) {// var res = JSON.parse(body);// console.log(res.timelineitems[0]);
// // for (var result = 1; result <= res.totalResults; result++) {// // console.log(res.timelineitems[0].object[result]);// // }
// })// }// });
// }// return;// });
// function ObjectLength( object ) {// var length = 0;// for( var key in object ) {// if( object.hasOwnProperty(key) ) {// ++length;// }// }// return length;// };