Skip to content

Instantly share code, notes, and snippets.

@Burbigo
Last active April 11, 2020 07:56
Show Gist options
  • Save Burbigo/5aee3ce0f7340ecdccb1e3bb6c77821c to your computer and use it in GitHub Desktop.
Save Burbigo/5aee3ce0f7340ecdccb1e3bb6c77821c to your computer and use it in GitHub Desktop.
const TelegramBot = require('node-telegram-bot-api');
const axios = require('axios');
// Telegram's token
const token = 'your_telegrams_token';
//OpenWeatherMap API key
const appID = 'your_openweathermap_api_key';
// OpenWeatherMap endpoint for getting weather by city name
const weatherEndpoint = (city) => (
`http://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&&appid=${appID}`
);
// URL that provides icon according to the weather
const weatherIcon = (icon) => `http://openweathermap.org/img/w/${icon}.png`;
// Template for weather response
const weatherHtmlTemplate = (name, main, weather, wind, clouds) => (
`The weather in <b>${name}</b>:
<b>${weather.main}</b> - ${weather.description}
Temperature: <b>${main.temp} °C</b>
Pressure: <b>${main.pressure} hPa</b>
Humidity: <b>${main.humidity} %</b>
Wind: <b>${wind.speed} meter/sec</b>
Clouds: <b>${clouds.all} %</b>
`
);
// Created instance of TelegramBot
const bot = new TelegramBot(token, {
polling: true
});
// Function that gets the weather by the city name
const getWeather = (chatId, city) => {
const endpoint = weatherEndpoint(city);
axios.get(endpoint).then((resp) => {
const {
name,
main,
weather,
wind,
clouds
} = resp.data;
bot.sendPhoto(chatId, weatherIcon(weather[0].icon))
bot.sendMessage(
chatId,
weatherHtmlTemplate(name, main, weather[0], wind, clouds), {
parse_mode: "HTML"
}
);
}, error => {
console.log("error", error);
bot.sendMessage(
chatId,
`Ooops...I couldn't be able to get weather for <b>${city}</b>`, {
parse_mode: "HTML"
}
);
});
}
// Listener (handler) for telegram's /weather event
bot.onText(/\/weather/, (msg, match) => {
const chatId = msg.chat.id;
const city = match.input.split(' ')[1];
if (city === undefined) {
bot.sendMessage(
chatId,
`Please provide city name`
);
return;
}
getWeather(chatId, city);
});
// Listener (handler) for telegram's /start event
// This event happened when you start the conversation with both by the very first time
// Provide the list of available commands
bot.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(
chatId,
`Welcome at <b>MyTestWeatherInfoBot</b>, thank you for using my service
Available commands:
/weather <b>city</b> - shows weather for selected <b>city</b>
`, {
parse_mode: "HTML"
}
);
});
@mezgoodle
Copy link

Hello!
Did you deploy this bot?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment