FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
All-In-One-Python-Projects/Telegram Bot/main.py at main · GHubPythonProjects/All-In-One-Python-Projects · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
GHubPythonProjects
/
All-In-One-Python-Projects
Public
forked from
king04aman/All-In-One-Python-Projects
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
All-In-One-Python-Projects
/
Telegram Bot
/
main.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
78 lines (60 loc) · 2.58 KB
Breadcrumbs
All-In-One-Python-Projects
/
Telegram Bot
/
main.py
Copy path
File metadata and controls
78 lines (60 loc) · 2.58 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Importing modules
import
os
,
requests
from
apscheduler
.
schedulers
.
background
import
BackgroundScheduler
import
telebot
# Golbal Variables
CITY_NAME
=
"Delhi,IN"
# Get Telegram Bot Token From Here: https://telegram.me/BotFather
BOT_TOKEN
=
os
.
environ
[
'BOT_TOKEN'
]
# Get Your API Key From Here: https://openweathermap.org/api
WEATHER_API_KEY
=
os
.
environ
[
'WEATHER_API_KEY'
]
scheduler
=
BackgroundScheduler
()
# Initialize bot object
bot
=
telebot
.
TeleBot
(
BOT_TOKEN
)
# get weather data
def
getWeather
():
url
=
f"https://api.openweathermap.org/data/2.5/weather?q=
{
CITY_NAME
}
&appid=
{
WEATHER_API_KEY
}
"
response
=
requests
.
get
(
url
)
weather_data
=
response
.
json
()
weather_text
=
""
if
weather_data
[
'cod'
]
==
200
:
weather_text
+=
f"City :
{
weather_data
[
'name'
]
}
,
{
weather_data
[
'sys'
][
'country'
]
}
\n
"
weather_text
+=
f"Coordinate :
{
weather_data
[
'coord'
][
'lon'
]
}
°N,
{
weather_data
[
'coord'
][
'lat'
]
}
°E
\n
"
weather_text
+=
f"Weather :
{
weather_data
[
'weather'
][
0
][
'main'
]
}
\n
"
weather_text
+=
f"Temperature :
{
weather_data
[
'main'
][
'temp'
]
}
°F
\n
"
weather_text
+=
f"Pressure :
{
weather_data
[
'main'
][
'pressure'
]
}
hPa
\n
"
weather_text
+=
f"Humidity :
{
weather_data
[
'main'
][
'humidity'
]
}
%
\n
"
weather_text
+=
f"Min-Temp :
{
weather_data
[
'main'
][
'temp_min'
]
}
°F
\n
"
weather_text
+=
f"Max-Temp :
{
weather_data
[
'main'
][
'temp_max'
]
}
°F
\n
"
weather_text
+=
f"Wind Speed :
{
weather_data
[
'wind'
][
'speed'
]
}
m/s
\n
"
weather_text
+=
f"Wind Direction :
{
weather_data
[
'wind'
][
'deg'
]
}
°
\n
"
weather_text
+=
f"Visibility :
{
weather_data
[
'visibility'
]
}
m
\n
"
else
:
weather_text
+=
f"Error:
{
weather_data
[
'message'
]
}
"
return
weather_text
# send weather data
def
sendWeather
(
message
):
weather_text
=
getWeather
()
bot
.
send_message
(
message
,
text
=
"The current weather details in Delhi is:
\n
\n
"
+
weather_text
)
# Start Weather updates
@
bot
.
message_handler
(
commands
=
[
'start'
])
def
start
(
message
):
bot
.
reply_to
(
message
,
"Weather updates started successfully."
)
scheduler
.
add_job
(
sendWeather
(
message
.
chat
.
id
),
'interval'
,
hours
=
1
)
scheduler
.
start
()
# Stop weather updates
@
bot
.
message_handler
(
commands
=
[
'stop'
])
def
stop
(
message
):
scheduler
.
remove_all_jobs
()
bot
.
send_message
(
message
.
chat
.
id
,
text
=
"Weather updates stopped successfully."
)
# Test command
@
bot
.
message_handler
(
commands
=
[
'test'
])
def
send_welcome
(
message
):
bot
.
reply_to
(
message
,
"Hello, I am ready to serve you."
)
# Set the listener
bot
.
set_update_listener
(
start
)
bot
.
set_update_listener
(
stop
)
# Run the bot
bot
.
infinity_polling
()
Back
|
FazBrowse Home
|
New Git URL