R
R
Ruslan2022-02-22 01:05:30
Python
Ruslan, 2022-02-22 01:05:30

Python - weather telegram bot?

Hello.
I started learning Python by writing a simple weather bot for TG and ran into a problem.
The bot is launched in the console, it does not give any errors, but it does not react to messages in the chat in any way.
At the same time, when I made an echo bot that repeated messages, everything worked correctly, the same with the weather, it worked correctly in the console, but as soon as they were combined, something broke.
Where did I go wrong?

Use pip pyTelegramBotAPI and pip pyowm

# telegramBot

import telebot
from pyowm import OWM
from pyowm.utils import config
from pyowm.utils import timestamps
from pyowm.utils.config import get_default_config

config_dict = get_default_config()
config_dict['language'] = 'ru' 

owm = OWM('your free OWM API key', config_dict)
mgr = owm.weather_manager() 
bot = telebot.TeleBot("TOKEN" )

@bot.message_handler(content_types=['text'])
def send_echo(message):
  observation = mgr.weather_at_place(message.text)
  w = observation.weather
  
  temp = w.temperature('celsius')["temp"]
  tempMin = w.temperature('celsius')["temp_min"]
  tempMax = w.temperature('celsius')["temp_max"]
  WindSpeed = w.wind()["speed"]

  answer = "В городе " + message.text + " сейчас " + w.detailed_status 
  answer += "\nТемпература на улице, примерно " + str(temp) + " градусов."
  answer += "\nМаксимальная температура " + str(tempMax) + " градусов." 
  answer += "\nМинимальная температура " + str(tempMin) + " градусов."
  answer += "\nСкорость ветра " + str(WindSpeed) + " метров в секунду.\n\n"


  if temp < 5:
    answer += "Сейчас довольно холодно, не забудь одеть шапку!"
  elif temp < 20:
    answer += "На улице прохладно, одевайся теплее."
  else:
    answer += "Температура комфортная для прогулки!"

  bot.send_message(message.chat.id, answer)

bot.polling(none_stop = True)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2022-02-22
@Razgildai

The bot is not running, there is no method to send a message to the user

J
JiMoon, 2022-02-22
@Jimoon

I will fix your code.

import telebot
from pyowm import OWM
from pyowm.utils import config
from pyowm.utils import timestamps
from pyowm.utils.config import get_default_config

config_dict = get_default_config()
config_dict['language'] = 'ru' 

owm = OWM('your free OWM API key', config_dict)
mgr = owm.weather_manager() 
bot = telebot.TeleBot("TOKEN" )

@bot.message_handler(commands=['start'])
def city(message):
    msg = bot.send_message(message.chat.id, "Пожалуйста, укажите ваш город."
    bot.register_next_step_handler(send_echo, msg)

def send_echo(message):
  observation = mgr.weather_at_place(message.text)
  w = observation.weather
  temp = w.temperature('celsius')["temp"]
  tempMin = w.temperature('celsius')["temp_min"]
  tempMax = w.temperature('celsius')["temp_max"]
  WindSpeed = w.wind()["speed"]

  answer = "В городе " + message.text + " сейчас " + w.detailed_status 
  answer += "\nТемпература на улице, примерно " + str(temp) + " градусов."
  answer += "\nМаксимальная температура " + str(tempMax) + " градусов." 
  answer += "\nМинимальная температура " + str(tempMin) + " градусов."
  answer += "\nСкорость ветра " + str(WindSpeed) + " метров в секунду.\n\n"


  if temp < 5:
    answer += "Сейчас довольно холодно, не забудь одеть шапку!"
  elif temp < 20:
    answer += "На улице прохладно, одевайся теплее."
  else:
    answer += "Температура комфортная для прогулки!"

bot.polling()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question