Search code examples
pythontelegram-botpython-telegram-botpy-telegram-bot-api

python telegram bot can't get movie data


am trying to do a telegram bot which shows to the user the popular movies upon sending ['pm']. But, I couldn't get the right message from the bot. Here is my code:

import os
import telebot
from dotenv import load_dotenv
import movies

load_dotenv()

API_KEYS = os.getenv("API_KEYS")
bot = telebot.TeleBot(API_KEYS)

pMovies = movies.mPopular

@bot.message_handler(commands=['Greet'])
def greet(message):
  bot.reply_to(message, "Hey! Hows it going?")

@bot.message_handler(commands=['pm'])
def hello(message):
  
    for p in pMovies:
            pid = p[0]
            ptitle = p[1]
            poverview = p[2]
            pvote_average = p[3]
            prelease_date = p[4]
            response = pid, '|',ptitle, '|', poverview, '|', pvote_average, '|', prelease_date
  
    bot.send_message(message.chat.id, response)

bot.polling()

Whenever I type 'pm' in telegram I only get one the first movie ID instead of getting the entire popular movies data. I Would be grateful if you can help me out with this.

Here is output am receiving:

enter image description here


Solution

  • In here bot.send_message(message.chat.id, list(response))

    send_message method takes second parameter(which is your message) as string. But your response variable is a tuple.

    1. You can convert your tuple to a string.

       response = pid, '|',ptitle, '|', poverview, '|', pvote_average, '|', prelease_date
       response_str=" ".join(response)
      

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

    or

    1. You can directly create a response string instead of tuple.

      response = f"{pid} | {ptitle} | {poverview} | {pvote_average} | {prelease_date}"