Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e75e818

Browse files
Merge pull request avinashkranjan#1807 from alwenpy/master
Telegram Weather Bot added
2 parents 09e0271 + 66429b9 commit e75e818

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

‎Telegram-Weather-Bot/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Prerequisites for running the bot
2+
- Python 3.6 or higher
3+
- pip3
4+
- python-telegram-bot
5+
- requests
6+
7+
## How to run the bot
8+
1. Clone the repository
9+
2. Open the terminal and navigate to the directory where the repository is cloned
10+
3. Run the following command to install the required packages
11+
```bash
12+
pip3 install -r requirements.txt
13+
```
14+
4. Add your bot token in the main.py file
15+
5. Run the following command to run the bot
16+
```bash
17+
python3 main.py
18+
```
19+
6. Open Telegram and search for the bot with the username you have given.
20+
7. Start the bot and send the location to get the weather forecast
21+
22+
## How to use the bot
23+
1. Send the location to the bot
24+
2. The bot will send the weather forecast for the given city
25+
3. The bot will send the temperature, wind direction, wind speed, humidity,pressure, sunrise ,sunset time and day length
26+
27+
28+
## Developer
29+
- [Arvind Srivastav]
30+

‎Telegram-Weather-Bot/main.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import requests
2+
import telebot
3+
4+
bot_token = 'YOUR_BOT_TOKEN'
5+
bot = telebot.TeleBot(bot_token)
6+
7+
8+
@bot.message_handler(commands=['start', 'help'])
9+
def send_welcome(message):
10+
bot.reply_to(message, "Send me a city name")
11+
12+
13+
@bot.message_handler(func=lambda m: True)
14+
def echo_all(message):
15+
response = requests.get("http://api.weatherapi.com/v1/current.json?key={}&q={}".format(
16+
"2d3f4a2bd175414aa45175205221408", message.text)).json()
17+
bot.send_message(
18+
message.chat.id, format_response_to_human_readable(response))
19+
20+
def format_response_to_human_readable(response):
21+
location = response["location"]
22+
current = response["current"]
23+
astronomy = response["forecast"]["forecastday"][0]["astro"]
24+
25+
return "Weather Information for {}\n"\
26+
"Temperature: {}°C\n"\
27+
"Wind: {} kph, {}\n"\
28+
"Humidity: {}%\n"\
29+
"Pressure: {} mb\n"\
30+
"Sunrise: {}\n"\
31+
"Sunset: {}\n"\
32+
"Day Length: {} hours {} minutes".format(
33+
location["name"],
34+
current["temp_c"],
35+
current["wind_kph"],
36+
current["wind_dir"],
37+
current["humidity"],
38+
current["pressure_mb"],
39+
astronomy["sunrise"],
40+
astronomy["sunset"],
41+
astronomy["sunrise"],
42+
astronomy["sunset"],
43+
astronomy["moon_phase"]
44+
)
45+
46+
47+
bot.infinity_polling()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /