-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
executable file
·59 lines (38 loc) · 2.38 KB
/
bot.py
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
import logging
from suntime import *
from weather import *
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
async def error(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
logger.warning('Update "%s" caused error "%s"', update, context.error)
async def sunrise(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
verify = get_sunrise()
await update.message.reply_text("Sunrise (Local time):\n" + str(verify))
async def sunset(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
verify = get_sunset()
await update.message.reply_text("Sunset (Local time):\n" + str(verify))
async def weather_temperature(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
verify = convertToCelsius(getWeatherTemperature(getJSON(response)))
await update.message.reply_text("Current temperature: " + str(verify) + "ºC ")
async def weather_humidity(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
verify = getWeatherHumidity(getWeatherTemperature(getJSON(response)))
await update.message.reply_text("Current humidity: " + str(verify) + "%")
async def show_help(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("You can use these commands: \n" + "/sunrise to check the sunrise time of the next day in Granada (in local time)\n" + "/sunset shows the sunset time in Granada\n" + "/temperature real time temperature in Granada\n" + "/humidity real time humidity in Granada\n")
def main():
application = Application.builder().token("your_token_here").build()
application.add_handler(CommandHandler("sunrise",sunrise))
application.add_handler(CommandHandler("sunset",sunset))
application.add_handler(CommandHandler("temperature",weather_temperature))
application.add_handler(CommandHandler("humidity",weather_humidity))
application.add_handler(CommandHandler("help",show_help))
application.add_error_handler(error)
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == '__main__':
main()