Телеграмм бот и модуль requests
Я сделал бота, который должен говорить информацию про персонажей star wars.
#import modules
import telebot
import requests
from django.http import response
#token
token = '6911144695:AAGhos7-Intqk4DC94ZuHeeZb9ZsjMYiGgI'
#connect token
bot = telebot.TeleBot(token)
#function for processing command /start
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, "Hi, I'm bot=helper about heroes of Star Wars!\n\nWrite a your hero name:", parse_mode='Markdown')
@bot.message_handler(content_types=['text'])
def starwars(message):
str(message.text)
url = 'https://swapi.dev/api/people/?search=' + message.text
response = requests.get(url)
answer = response.json()['results'][0]
name = answer['name']
mass = answer['mass']
height = answer['height']
hair_color = answer['hair_color']
skin_color = answer['skin_color']
eye_color = answer['eye_color']
birth_year = answer['birth_year']
gender = answer['gender']
bot.send_message(message.chat.id, "Name:",name)
bot.send_message(message.chat.id, "Mass:",mass)
bot.send_message(message.chat.id, "Height:",height)
bot.send_message(message.chat.id, "Hair color:",hair_color)
bot.send_message(message.chat.id, "Skin color:",skin_color)
bot.send_message(message.chat.id, "Eye color:",eye_color)
bot.send_message(message.chat.id, "Birth year:",birth_year)
bot.send_message(message.chat.id, "Gender:",gender)
if __name__ == '__main__':
#infinity bot cheking
while True:
#chek bot msg
try:
bot.polling(none_stop=True, interval=0)
except Exception as e:
print('ЭТОТ ЧЕЛ НЕ РУССКИЙ, ОСТАНОВИ ЕГО!!!!!!!!!!!!!!!!!!!!!!!!!!!')
Тут не работают запросы, что можно сделать чтобы бот заработал?
Ответы (1 шт):
Автор решения: ZxNuClear
→ Ссылка
Вы не правильно "отдаете" полученный ответ пользователю. В том виде, в котором вы передавали переменные воспринимаются как parse_mode, а не как часть строки. Вот так должно работать:
@bot.message_handler(content_types=['text'])
def starwars(message):
str(message.text)
url = 'https://swapi.dev/api/people/?search=' + message.text
response = requests.get(url)
answer = response.json()['results'][0]
name = answer['name']
mass = answer['mass']
height = answer['height']
hair_color = answer['hair_color']
skin_color = answer['skin_color']
eye_color = answer['eye_color']
birth_year = answer['birth_year']
gender = answer['gender']
bot.send_message(message.chat.id, f"Name: {name}\nMass: {mass}\nHeight: {height}\nHair color: {hair_color}\nSkin color: {skin_color}\nEye color: {eye_color}\nBirth year: {birth_year}\nGender: {gender}")
ну и желательно отдавать все одним сообщением, чтобы бот не "спамил"