M
M
Muck Runout2022-02-22 22:34:16
Python
Muck Runout, 2022-02-22 22:34:16

The bot command is not working correctly, how to fix it?

I use the TELEBOT library.
Imported bs4 requests telebot.
The token was added and the handler too.
It does not give any errors when starting, all commands work except this one:

def getanekdot():
    z=''
    s=requests.get('http://anekdotme.ru/random')
    b=bs4.BeautifulSoup(s.text, "html.parser")
    p=b.select('.anekdot_text')
    for x in p:        
        s=(x.getText().strip())
        z=z+s+'\n\n'
    return s
    
def handle_text(message):
    msg=message.text
    msg=msg.lower()
    if message.text == 'анекдот':
            bot.send_message(message.chat.id, getanekdot())

It works without errors, but if you write a joke to the bot, it is silent.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Musickk, 2022-02-22
@Musickk

you forgot to write content_types, that's your mistake :)

import telebot
from telebot import types
import bs4
from bs4 import BeautifulSoup
import requests


def getanekdot():
    z=''
    s=requests.get('http://anekdotme.ru/random')
    b=bs4.BeautifulSoup(s.text, "html.parser")
    p=b.select('.anekdot_text')
    for x in p:        
        s=(x.getText().strip())
        z=z+s+'\n\n'
    return s

@bot.message_handler(content_types='text')
def handle_text(message):
    msg=message.text
    msg=msg.lower()
    if message.text == 'анекдот':
        bot.send_message(message.chat.id, getanekdot())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question