-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path47 - Telegram Bot with Python.py
More file actions
51 lines (38 loc) · 1.46 KB
/
47 - Telegram Bot with Python.py
File metadata and controls
51 lines (38 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from telegram.ext import Updater, CommandHandler
import requests
BOT_TOKEN = "**************************************************" # no spaces
WEATHER_API_KEY = "****************************************"
def start(update, context):
update.message.reply_text(
"Hello 👋\nUse:\n/weather <city>\nExample: /weather Delhi"
)
def weather(update, context):
if len(context.args) == 0:
update.message.reply_text("Please provide a city name.\nExample: /weather Mumbai")
return
city = " ".join(context.args)
url = (
f"https://api.openweathermap.org/data/2.5/weather"
f"?q={city}&appid={WEATHER_API_KEY}&units=metric"
)
response = requests.get(url).json()
if response.get("cod") != 200:
update.message.reply_text("❌ City not found")
return
temp = response["main"]["temp"]
feels = response["main"]["feels_like"]
desc = response["weather"][0]["description"]
reply = (
f"🌍 City: {city}\n"
f"🌡 Temperature: {temp}°C\n"
f"🤒 Feels like: {feels}°C\n"
f"☁️ Condition: {desc}"
)
update.message.reply_text(reply)
updater = Updater(BOT_TOKEN, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("weather", weather))
print("🤖 Bot is running...")
updater.start_polling()
updater.idle()