How can i upload and read my excel file on ibm cloud function?

29 Views Asked by At

im programming a telegram bot that automates sending message of the day

Below is the code ive written on google colab which is working. However i want to host it on ibm cloud, which triggers everyday such that it automatically sends 1 message in the list each day. How can i upload the excel file and also read the file to fit my requirements?

import time
import openpyxl


base_url = "https://api.telegram.org/bot6834036173:AAGQ2f_R29AKp27A69CA59mCctMQSWEIceg/sendMessage"

message = ["1","2", "3","4"]

def read_counter(excel_file, sheet_name, cell):
    try:
        workbook = openpyxl.load_workbook(excel_file)
        sheet = workbook[sheet_name]
        counter_value = sheet[cell].value
        return counter_value
    except FileNotFoundError:
        print("Excel file not found.")
    except Exception as e:
        print(f"Error reading counter: {e}")

def update_counter(excel_file, sheet_name, cell, new_value):
    try:
        workbook = openpyxl.load_workbook(excel_file)
        sheet = workbook[sheet_name]
        sheet[cell].value = new_value
        workbook.save(excel_file)
        print("Counter updated successfully.")
    except FileNotFoundError:
        print("Excel file not found.")
    except Exception as e:
        print(f"Error updating counter: {e}")

# Example usage
excel_file_path = "/content/Counter.xlsx"
sheet_name = "Sheet1"
counter_cell = "A1"

# Read the current counter value
current_counter = read_counter(excel_file_path, sheet_name, counter_cell)
print("Current counter value:", current_counter)

parameters = {
  "chat_id" : "-1002013558877",
  "text" : message[current_counter]
  }

resp = requests.get(base_url, data=parameters)

# Update the counter (increment by 1, for example)
if current_counter == 13:
  new_counter_value = 0
else:
  new_counter_value = current_counter + 1
update_counter(excel_file_path, sheet_name, counter_cell, new_counter_value)```
0

There are 0 best solutions below