Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 50a9543

Browse files
authored
added files
1 parent 5ed5209 commit 50a9543

File tree

10 files changed

+125
-0
lines changed

10 files changed

+125
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Telegram Weather Bot
2+
3+
Telegram bot that provides real time weather info of a particular city using Open Weahter Maps API
4+
<p align="center">
5+
<img src='./bot_test.jpg' width=275 />
6+
</p>
7+
8+
## How to run
9+
10+
### Requirements
11+
- `pip` install the packages in requirements.txt
12+
- Stable internet connnection required
13+
14+
### Creating the Bot
15+
- Got to telegram
16+
- Search for 'BotFather' and click on the blue ticked account
17+
- Follow the instructions given to create a new bot
18+
- Copy the key and replace the given key with `YOUR_KEY` in telegram_config.cfg
19+
20+
### Getting API Key
21+
- Sign up on [Open Weather](https://openweathermap.org/) for free
22+
- Click on your username and go to 'My API Keys' section
23+
- Copy the API key and replace the given key with `YOUR_KEY` in owm_config.cfg
24+
25+
### Executing the Program
26+
Open your command prompt and switch to the directory where all files are stored and run the command below
27+
<br>`$ python server.py`
28+
29+
## Author
30+
31+
[Naman Shah](https://github.com/namanshah01)
Binary file not shown.
Binary file not shown.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import requests
2+
import json
3+
import configparser as cfg
4+
5+
class telegram_chatbot():
6+
7+
def __init__(self, config):
8+
self.token = self.read_config(config)
9+
self.base = f'https://api.telegram.org/bot{self.token}/'
10+
11+
def get_updates(self, offset=None):
12+
url = self.base + 'getUpdates?timeout=100'
13+
if offset:
14+
url = url + f'&offset={offset+1}'
15+
res = requests.get(url)
16+
return json.loads(res.content)
17+
18+
def send_message(self, msg, chat_id):
19+
url = self.base + f'sendMessage?chat_id={chat_id}&text={msg}'
20+
if msg is not None:
21+
requests.get(url)
22+
23+
def read_config(self, config):
24+
parser = cfg.ConfigParser()
25+
parser.read(config)
26+
return parser.get('creds', 'token')
374 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[creds]
2+
token = YOUR_KEY
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests==2.23.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from bot import telegram_chatbot
2+
from weather import weather_info
3+
4+
bot = telegram_chatbot('telegram_config.cfg')
5+
wi = weather_info('owm_config.cfg')
6+
7+
def make_reply(message):
8+
reply = None
9+
if message is not None:
10+
stats = wi.get_info(message)
11+
if message == '/start' or message == '/help':
12+
reply = "Enter a city name in chat to get its real time weather stats\nSend '/help' in case of unexpected behaviour or difficulties\n\nNote: Make sure you enter a 'valid city name' and not any state or country name, doing so will result in improper replies"
13+
return reply
14+
if str(stats['cod']) == '404':
15+
return 'City not found. Enter a valid city name'
16+
reply = []
17+
reply.append(stats['name'])
18+
reply.append(f"Description: {stats['weather'][0]['description']}")
19+
reply.append(f"Temperature(celcius): {round(stats['main']['temp'] - 273.15, 2)}")
20+
reply.append(f"Max Temperature(celcius): {round(stats['main']['temp_max'] - 273.15, 2)}")
21+
reply.append(f"Min Temperature(celcius): {round(stats['main']['temp_min'] - 273.15, 2)}")
22+
reply.append(f"Humidity(percent): {stats['main']['humidity']}")
23+
reply.append(f"Pressure(hPa): {stats['main']['pressure']}")
24+
reply = '\n'.join(reply)
25+
return reply
26+
27+
update_id = None
28+
while True:
29+
print('...')
30+
updates = bot.get_updates(offset=update_id)
31+
updates = updates['result']
32+
if updates:
33+
for item in updates:
34+
update_id = item['update_id']
35+
try:
36+
message = item['message']['text']
37+
except:
38+
message = None
39+
from_ = item['message']['chat']['id']
40+
reply = make_reply(message)
41+
bot.send_message(reply, from_)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[creds]
2+
token = YOUR_KEY
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import requests
2+
import json
3+
import configparser as cfg
4+
5+
class weather_info():
6+
7+
def __init__(self, config):
8+
self.token = self.read_config(config)
9+
self.base = f'https://api.openweathermap.org/data/2.5/weather?appid={self.token}'
10+
11+
def get_info(self, city):
12+
url = self.base + f'&q={city}'
13+
res = requests.get(url)
14+
return json.loads(res.content)
15+
16+
def read_config(self, config):
17+
parser = cfg.ConfigParser()
18+
parser.read(config)
19+
return parser.get('creds', 'token')
20+
21+
# w = weather_info('owm_config.cfg')
22+
# print(w.get_info('mumbai'))

0 commit comments

Comments
 (0)