Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

telegram weather bot added #198

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Telegram-Weather-Bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Prerequisites for running the bot
- Python 3.6 or higher
- pip3
- python-telegram-bot
- requests
-telebot

## How to run the bot
1. Clone the repository
2. Open the terminal and navigate to the directory where the repository is cloned
3. Run the following command to install the required packages
```bash
pip3 install -r requirements.txt
```
4. Add your bot token in the main.py file
5. Run the following command to run the bot
```bash
python3 main.py
```
6. Open Telegram and search for the bot with the username you have given.
7. Start the bot and send the location to get the weather forecast

## How to use the bot
1. Send the location to the bot
2. The bot will send the weather forecast for the given city
3. The bot will send the temperature, wind direction, wind speed, humidity,pressure, sunrise ,sunset time and day length


## Developer
- [Arvind Srivastav]

## Github Repo
- [Weather-Bot](https://github.com/alwenpy/mausam_bot)

## Telegram Bot
- [Mausam Bot](https://t.me/yourmausambot)

47 changes: 47 additions & 0 deletions Telegram-Weather-Bot/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import requests
import telebot

bot_token = 'YOUR_BOT_TOKEN'
bot = telebot.TeleBot(bot_token)


@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.reply_to(message, "Send me a city name")


@bot.message_handler(func=lambda m: True)
def echo_all(message):
response = requests.get("http://api.weatherapi.com/v1/current.json?key={}&q={}".format(
"2d3f4a2bd175414aa45175205221408", message.text)).json()
bot.send_message(
message.chat.id, format_response_to_human_readable(response))

def format_response_to_human_readable(response):
location = response["location"]
current = response["current"]
astronomy = response["forecast"]["forecastday"][0]["astro"]

return "Weather Information for {}\n"\
"Temperature: {}°C\n"\
"Wind: {} kph, {}\n"\
"Humidity: {}%\n"\
"Pressure: {} mb\n"\
"Sunrise: {}\n"\
"Sunset: {}\n"\
"Day Length: {} hours {} minutes".format(
location["name"],
current["temp_c"],
current["wind_kph"],
current["wind_dir"],
current["humidity"],
current["pressure_mb"],
astronomy["sunrise"],
astronomy["sunset"],
astronomy["sunrise"],
astronomy["sunset"],
astronomy["moon_phase"]
)


bot.infinity_polling()