-1

I am making a telegram bot and it takes the data of movies and puts it in a csv file which i later give for exporting, but in my code the csv file is not getting created and if i add a pre added csv file, the data is not getting exported to it. here is the full code

import os
import telebot
import requests
import json
import csv
from dotenv import load_dotenv
load_dotenv()
# TODO: 1.1 Get your environment variables 
bot_id = os.getenv('botkey')
my_key = os.getenv('keyy')
bot = telebot.TeleBot(bot_id)

@bot.message_handler(commands=['start', 'hello'])
def greet(message):
    global botRunning
    botRunning = True
    bot.reply_to(
        message, 'Hello there! I am a bot that will show mov information for you and export it in a CSV file.\n\n')
    
@bot.message_handler(commands=['stop', 'bye'])
def goodbye(message):
    global botRunning
    botRunning = False
    bot.reply_to(message, 'Bye!\nHave a good time')
    


@bot.message_handler(func=lambda message: botRunning, commands=['help'])
def helpProvider(message):
    bot.reply_to(message, '1.0 You can use \"/movie MOVIE_NAME\" command to get the details of a particular mov. For eg: \"/movie The Shawshank Redemption\"\n\n2.0. You can use \"/export\" command to export all the mov data in CSV format.\n\n3.0. You can use \"/stop\" or the command \"/bye\" to stop the bot.')


@bot.message_handler(func=lambda message: botRunning, commands=['movie'])
def getMovie(message):
    bot.reply_to(message, 'Getting movie info...')
    mov = message.text
    mov = mov.replace("/movie","")
    response = requests.get(f'http://www.omdbapi.com/?apikey=a13fc218&t={mov}')
    mov_data=response.json()
    print(json.dumps(mov_data,indent = 4))
    bot.reply_to(message,f"{mov_data['Poster']} \nMovie Name: {mov_data['Title']} \nYear: {mov_data['Year']} \nReleased: {mov_data['Released']} \nImdb Rating: {mov_data['imdbRating']}")
    bot.send_photo(message,{mov_data['Poster']})
    with open('movies.csv','a',encoding='UTF8',newline='') as f:
        writer = csv.writer(f)
        writer.writerow([mov_data['Title'],mov_data['Year'],mov_data['Released'],mov_data['imdbRating']])


  
@bot.message_handler(func=lambda message: botRunning, commands=['export'])
def getList(message):
    bot.reply_to(message, 'Generating file...')
    chat_id = message.chat.id 
    print()
    mov_data=open('movies.csv','rb')
    bot.send_document(chat_id,mov_data)
    

@bot.message_handler(func=lambda message: botRunning)
def default(message):
    bot.reply_to(message, 'I did not understand '+'\N{confused face}')
    
bot.infinity_polling()

Yes, i have tried to make the location of the creation of file very specific, that didnt work as well. I am working on a macbook. Need help

  • Please provide a [_minimal_ reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Jorge Luis Mar 09 '23 at 15:22
  • what is the purpose of `load_dotenv`? – rv.kvetch Mar 09 '23 at 15:24
  • also, `TODO: 1.1 Get your environment variables` - we don't have these – rv.kvetch Mar 09 '23 at 15:24
  • 1
    its for using the botid and the apikey using environment variables, i am concerned about the csv part i asked in the question – Sai Ganesh K Mar 09 '23 at 16:24
  • Is the problem with `getMovie()` or `getList()`? – JonSG Mar 09 '23 at 16:40
  • @JonSG, the problem starts with the getMovie() and since the execution of getMovie() is not happening, getList() doesnt work either – Sai Ganesh K Mar 09 '23 at 16:50
  • Seems to work for me in a little test, what `message` seems to result in a failure for you? Is it a movie that does not exists and you get a keyerror? – JonSG Mar 09 '23 at 16:53
  • The movie details are shown, but when i put the /export command, the csv file is not getting created where the movie data we got has to be stored. it just shows what i have put in the heading – Sai Ganesh K Mar 09 '23 at 17:50

1 Answers1

0

Does this help if you define that parameters explicitly?

def getList(message):
    with open('movies.csv','rb') as movie_csv_file:
        bot.send_document(
            chat_id=message.chat.id ,
            document=movie_csv_file
            filename="all_movies.csv"
        )
JonSG
  • 10,542
  • 2
  • 25
  • 36