Answer the question
In order to leave comments, you need to log in
The function in the telegram bot on python does not work, how to fix it?
I am making a python bot in telegram. I made a dollar rate parser, when I run it through the console everything works fine. I upload it to PythonAnyWhere, it stops responding to this command. How to fix it?
Here is the code:
import telebot
import random
import wikipedia
import config
import time
import requests
from telebot import types
from covid import Covid
from bs4 import BeautifulSoup
bot = telebot.TeleBot(config.token)
play = ["Бумага", "Ножницы", "Камень"]
covid = Covid()
wikipedia.set_lang("RU")
@bot.message_handler(commands=['start'])
def welcome(message):
sti = open('static/welcome.tgs', 'rb')
bot.send_sticker(message.chat.id, sti)
# keyboard
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton(" Курс доллара")
item2 = types.KeyboardButton(" Как дела?")
item3 = types.KeyboardButton("✂ Камень, ножницы, бумага")
item4 = types.KeyboardButton(" Данные об Коронавирусе")
item5 = types.KeyboardButton(" Википедия")
markup.add(item1, item2, item3, item4, item5)
bot.send_message(message.chat.id, "Добро пожаловать, {0.first_name}!\nЯ - <b>{1.first_name}</b>, бот созданный чтобы быть подопытным кроликом.".format(message.from_user, bot.get_me()),
parse_mode='html', reply_markup=markup)
@bot.message_handler(content_types=['text'])
def task(message):
try:
if message.chat.type == 'private':
#тут проблема
if message.text == ' Курс доллара':
kurs = open('static/kurs.tgs', 'rb')
bot.send_sticker(message.chat.id, kurs)
DOLLAR_RUB = 'https://www.google.com/search?q=%D0%BA%D1%83%D1%80%D1%81+%D0%B4%D0%BE%D0%BB%D0%BB%D0%B0%D1%80%D0%B0&rlz=1C1DVJR_ruRU904RU904&oq=%D0%BA%D1%83%D1%80%D1%81+%D0%B4%D0%BE%D0%BB%D0%BB%D0%B0%D1%80%D0%B0+&aqs=chrome..69i57j69i59j0l5j69i60.3872j1j7&sourceid=chrome&ie=UTF-8'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'}
full_page = requests.get(DOLLAR_RUB, headers=headers)
soup = BeautifulSoup(full_page.content, 'html.parser')
convert = soup.findAll("span", {"class": "DFlfde", "class": "SwHCTb", "data-precision": 2})
bot.send_message(message.chat.id, 'Сейчас курс доллара: ' + convert[0].text + ' рублей.')
elif message.text == ' Как дела?':
dela = open('static/dela.tgs', 'rb')
bot.send_sticker(message.chat.id, dela)
markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton("Хорошо", callback_data='good')
item2 = types.InlineKeyboardButton("Не очень", callback_data='bad')
markup.add(item1, item2)
bot.send_message(message.chat.id, 'Отлично, сам как?', reply_markup=markup)
elif message.text == '✂ Камень, ножницы, бумага':
play = open('static/play.tgs', 'rb')
bot.send_sticker(message.chat.id, play)
markup = types.InlineKeyboardMarkup(row_width=3)
item1 = types.InlineKeyboardButton("Камень", callback_data='rock')
item2 = types.InlineKeyboardButton("Ножницы", callback_data='scissors')
item3 = types.InlineKeyboardButton("Бумага", callback_data='paper')
markup.add(item1, item2, item3)
bot.send_message(message.chat.id, 'Давай сыграем в игру "Камень, ножницы, бумага". Напиши свой ход и я его сделаю))', reply_markup=markup)
elif message.text == ' Данные об Коронавирусе':
virus = open('static/virus.tgs', 'rb')
bot.send_sticker(message.chat.id, virus)
bot.send_message(message.chat.id, 'Заражено - ' + str(covid.get_total_confirmed_cases()) + '\nВыздоровело - ' + str(covid.get_total_recovered()) + '\nУмерло - ' + str(covid.get_total_deaths()))
elif message.text == ' Википедия':
pep = open('static/pep.tgs', 'rb')
bot.send_sticker(message.chat.id, pep)
bot.send_message(message.chat.id, 'Введите /Вики и то что хотите узнать')
elif '/Вики' in message.text:
search_query = message.text.replace('/Вики ', '')
search_result = str(wikipedia.summary(search_query))
wmeski = "Вот что я нашёл: \n{}".format(search_result)
bot.send_message(message.chat.id, wmeski)
else:
bot.send_message(message.chat.id, 'Я не знаю что ответить ')
except Exception as E:
time.sleep(1)
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
try:
if call.message:
if call.data == 'good':
bot.send_message(call.message.chat.id, 'Вот и отличненько ')
elif call.data == 'bad':
bot.send_message(call.message.chat.id, 'Бывает ')
elif call.data == 'rock':
bot.send_message(call.message.chat.id, random.choice(play) + '. Как думаешь, кто выиграл? ')
elif call.data == 'scissors':
bot.send_message(call.message.chat.id, random.choice(play) + '. Как думаешь, кто выиграл? ')
elif call.data == 'paper':
bot.send_message(call.message.chat.id, random.choice(play) + '. Как думаешь, кто выиграл? ')
except Exception as e:
print(repr(e))
# RUN
bot.polling(none_stop=True)
Answer the question
In order to leave comments, you need to log in
I may be wrong , but on pythonanywhere, you need to use webhooks to host a bot.
Here is the tutorial:
https://blog.pythonanywhere.com/148/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question