Skip to content

Commit 6985d18

Browse files
committed
feat: Added telegram bot using pyTelegramBotAPI
1 parent 0775110 commit 6985d18

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

Telegram Bot/main.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Importing modules
2+
import os, requests
3+
from apscheduler.schedulers.background import BackgroundScheduler
4+
import telebot
5+
6+
# Golbal Variables
7+
CITY_NAME = "Delhi,IN"
8+
# Get Telegram Bot Token From Here: https://telegram.me/BotFather
9+
BOT_TOKEN = os.environ['BOT_TOKEN']
10+
# Get Your API Key From Here: https://openweathermap.org/api
11+
WEATHER_API_KEY = os.environ['WEATHER_API_KEY']
12+
13+
scheduler = BackgroundScheduler()
14+
15+
# Initialize bot object
16+
bot = telebot.TeleBot(BOT_TOKEN)
17+
18+
19+
# get weather data
20+
def getWeather():
21+
url = f"https://api.openweathermap.org/data/2.5/weather?q={CITY_NAME}&appid={WEATHER_API_KEY}"
22+
response = requests.get(url)
23+
weather_data = response.json()
24+
25+
weather_text = ""
26+
27+
if weather_data['cod'] == 200:
28+
weather_text += f"City : {weather_data['name']}, {weather_data['sys']['country']}\n"
29+
weather_text += f"Coordinate : {weather_data['coord']['lon']} °N, {weather_data['coord']['lat']} °E\n"
30+
weather_text += f"Weather : {weather_data['weather'][0]['main']}\n"
31+
weather_text += f"Temperature : {weather_data['main']['temp']} °F\n"
32+
weather_text += f"Pressure : {weather_data['main']['pressure']} hPa\n"
33+
weather_text += f"Humidity : {weather_data['main']['humidity']} %\n"
34+
weather_text += f"Min-Temp : {weather_data['main']['temp_min']} °F\n"
35+
weather_text += f"Max-Temp : {weather_data['main']['temp_max']} °F\n"
36+
weather_text += f"Wind Speed : {weather_data['wind']['speed']} m/s\n"
37+
weather_text += f"Wind Direction : {weather_data['wind']['deg']}°\n"
38+
weather_text += f"Visibility : {weather_data['visibility']} m\n"
39+
else:
40+
weather_text += f"Error: {weather_data['message']} "
41+
42+
return weather_text
43+
44+
45+
# send weather data
46+
def sendWeather(message):
47+
weather_text = getWeather()
48+
bot.send_message(
49+
message,
50+
text="The current weather details in Delhi is: \n\n" + weather_text)
51+
52+
53+
# Start Weather updates
54+
@bot.message_handler(commands=['start'])
55+
def start(message):
56+
bot.reply_to(message, "Weather updates started successfully.")
57+
scheduler.add_job(sendWeather(message.chat.id), 'interval', hours=1)
58+
scheduler.start()
59+
60+
61+
# Stop weather updates
62+
@bot.message_handler(commands=['stop'])
63+
def stop(message):
64+
scheduler.remove_all_jobs()
65+
bot.send_message(message.chat.id, text="Weather updates stopped successfully.")
66+
67+
68+
# Test command
69+
@bot.message_handler(commands=['test'])
70+
def send_welcome(message):
71+
bot.reply_to(message, "Hello, I am ready to serve you.")
72+
73+
74+
# Set the listener
75+
bot.set_update_listener(start)
76+
bot.set_update_listener(stop)
77+
# Run the bot
78+
bot.infinity_polling()

0 commit comments

Comments
 (0)