├── README.md ├── main.py └── weather.py /README.md: -------------------------------------------------------------------------------- 1 | # telebot-python 2 | telegram bot using python and telegram bot API 3 | 4 | #packages you need to install 5 | 6 | $ pip install pyTelegramBotAPI 7 | 8 | $ pip install python-decouple 9 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #main commands and bot creation 2 | 3 | import telebot 4 | from decouple import config 5 | from weather import getCurrentWeather 6 | BOT_TOKEN = config('BOT_TOKEN') 7 | bot = telebot.TeleBot(BOT_TOKEN) 8 | 9 | weather = ["weather","temp","temprature"] 10 | greetings = ["hello","hi","hey"] 11 | whoAreYou = ["who" , "what" ] 12 | botName = "billy" 13 | 14 | @bot.message_handler(commands=["start","help"]) 15 | def welcome(message): 16 | bot.send_message(message.chat.id,"welcome to billy bot just a bot to have fun building it") 17 | 18 | #answering every message not just commands 19 | def isMSg(message): 20 | return True 21 | 22 | 23 | @bot.message_handler(func=isMSg) 24 | def reply(message): 25 | words = message.text.split() 26 | if words[0].lower() in weather : 27 | report = getCurrentWeather() 28 | return bot.send_message(message.chat.id,report or "failed to get weather !!") 29 | if words[0].lower() in whoAreYou : 30 | return bot.reply_to(message,f"i am {botName}") 31 | if words[0].lower() in greetings : 32 | return bot.reply_to(message,"hey how is going!") 33 | else: 34 | return bot.reply_to(message,"that's not a command of mine!") 35 | 36 | bot.polling() 37 | -------------------------------------------------------------------------------- /weather.py: -------------------------------------------------------------------------------- 1 | import requests, json 2 | from decouple import config 3 | 4 | WEATHER_API_KEY = config('WEATHER') 5 | 6 | BASE_URL = "https://api.openweathermap.org/data/2.5/weather?" 7 | 8 | 9 | class City: 10 | def __init__(self,name,lon,lat): 11 | self.name = name #name of the city 12 | self.lon = lon #longitude 13 | self.lat = lat #latitude 14 | 15 | #تكدر تعرف اي مدينة من هذا الكلاس 16 | 17 | basra = City("Basra ,IQ","47.783489","30.508102") 18 | 19 | baghdad = City("Baghdad, IQ","44.361488","30.508102") 20 | 21 | 22 | 23 | limit = 5 24 | URL = BASE_URL + "lat=" + basra.lat + "&lon=" + basra.lon +"&lang=ar"+"&units=metric"+ "&appid=" + WEATHER_API_KEY 25 | 26 | # HTTP request 27 | def getCurrentWeather(): 28 | 29 | response = requests.get(URL) 30 | # checking the status code of the request 31 | if response.status_code == 200: 32 | # getting data in the json format 33 | data = response.json() 34 | 35 | # getting the main dict block 36 | main = data['main'] 37 | # getting temperature 38 | temperature = main['temp'] 39 | # getting the humidity 40 | humidity = main['humidity'] 41 | # getting the pressure 42 | pressure = main['pressure'] 43 | # weather report 44 | report = data['weather'] 45 | # print(f"{CITY:-^30}") 46 | # print(f"temp : {temperature} °C") 47 | # print(f"humind : {humidity} %") 48 | # print(f"prusser : {pressure} hPa") 49 | # print(f"report : {report[0]['description']} ") 50 | 51 | return f""" \n 52 | {basra.name:-^30} \n 53 | درجة الحرارة : {temperature} °C\n 54 | الرطوبة : {humidity} %\n 55 | الضغط : {pressure} hPa\n 56 | {report[0]['description']} 57 | 58 | 59 | """ 60 | else: 61 | # showing the error message 62 | print("Error in the HTTP request") 63 | --------------------------------------------------------------------------------