Data not saved after running a scheduler on a Telegram bot

For personal work, I’m building a Telegram Bot that acts as a calendar for my favorite team’s matches. The idea for this Bot was to scrape from a site that publishes the match calendar, put this calendar in a local database (sqlite3) and access it via the commands on Telegram, /prossime, /prossime10, /calendario. Another task I would like you to do is that if you find a match on today’s date (after checking with the scheduler) you must send me a message on a Telegram channel; however, the issues I have encountered so far are:

  1. Although the Bot connects to the website to do scraping, I find that until I stop the bot manually the data is not saved in the database. I therefore find myself having to do CTRL + C every time to stop the Bot and save the data, which can then be easily accessed by the Bot. Do you have any idea how to solve it?

  2. I can’t send messages with the send_match_reminder() function either privately or on the channel. Here too, do you have any ideas?

  3. Finally, I can’t get the Bot to work on the channel, even though I send commands, the Bot doesn’t respond. Something is probably missing.

I leave much of the code for you to understand. I will also send more eventually.

import asyncio
import requests
import telegram
import os
import string
import locale
import random
import logging
import schedule
import time
import sqlite3
from lxml import html
from datetime import datetime
from dotenv import load_dotenv
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, CallbackContext, CallbackQueryHandler

load_dotenv()

CONNESSIONE_DB = sqlite3.connect('JUVENTUS_MATCHES.db')

CONNESSIONE = CONNESSIONE_DB.cursor()

locale.setlocale(locale.LC_TIME, 'it_IT.UTF-8')

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', filename='BOT_JUVE.log')

logger = logging.getLogger(__name__)



async def start_bot(update: Update, context: CallbackContext):
    await update.message.reply_text("HI!")

              .
              .
              .
              .
              .

async def get_next_match():
    CONNESSIONE.execute("SELECT * FROM MATCHES ORDER BY DATA")
    MATCHES = CONNESSIONE.fetchall()
    TODAY = datetime.today().strftime("%Y-%m-%d")

    for MATCH in MATCHES:
        if MATCH[2] >= TODAY:
            return MATCH

    return None

async def get_next_10_matches():
    CONNESSIONE.execute("SELECT * FROM MATCHES ORDER BY DATA")
    MATCHES = CONNESSIONE.fetchall()
    TODAY = datetime.today().strftime("%Y-%m-%d")

    NEXT_10_MATCHES = []
    for MATCH in MATCHES:
        if MATCH[2] >= TODAY:
            NEXT_10_MATCHES.append(MATCH)
            if len(NEXT_10_MATCHES) == 10:
                break

    return NEXT_10_MATCHES  

async def get_all_match():
    CONNESSIONE.execute("SELECT * FROM MATCHES ORDER BY DATA")
    MATCHES = CONNESSIONE.fetchall()
    TODAY = datetime.today().strftime("%Y-%m-%d")

    ALL_MATCHES = []
    for MATCH in MATCHES:     
        ALL_MATCHES.append(MATCH)

    return ALL_MATCHES

              .
              .
              .
              .
              .

async def send_match_reminder(update: Update, context: CallbackContext):
    MATCH = await get_next_match()
    if MATCH:
        MESSAGE = f"⚠️ OGGI GIOCHIAMO! ⚠️nn"
        if MATCH[1] == "Serie A":
            MESSAGE += f"???? {MATCH[1]} - {MATCH[4]}n⚽️ {MATCH[2]}n???? {datetime.strptime(MATCH[3], '%Y-%m-%d').strftime('%A %d %B %Y')}n???? {MATCH[5]}nn"
        else:
            MESSAGE += f"???? {MATCH[1]}n⚽️ {MATCH[2]}n???? {datetime.strptime(MATCH[3], '%Y-%m-%d').strftime('%A %d %B %Y')}n???? {MATCH[5]}nn"

        await context.bot.send_message(chat_id=update.effective_chat.id, text=MESSAGE, parse_mode="HTML")

    else:
        MESSAGE = f"<b>❗️ NON CI SONO PARTITE IN PROGRAMMA ❗️</b>"
        await context.bot.send_message(chat_id=update.effective_chat.id, text=MESSAGE, parse_mode="HTML")

              .
              .
              .
              .
              .

async def update_database():
    MATCHES = await get_juventus_matches()

    CONNESSIONE.execute('DROP TABLE IF EXISTS MATCHES')

    CONNESSIONE.execute('CREATE TABLE MATCHES (TIPO TEXT, PARTITA TEXT, DATA DATE, ORA TEXT, GIORNATA TEXT)')

    CONNESSIONE.executemany("INSERT INTO MATCHES (TIPO, PARTITA, DATA, ORA, GIORNATA) VALUES (?, ?, ?, ?, ?)", [(MATCH['TIPO'], MATCH['PARTITA'], MATCH['DATA'], MATCH['ORA'], MATCH['GIORNATA']) for MATCH in MATCHES])

    CONNESSIONE_DB.commit()

schedule.every().day.at("10:45").do(lambda: asyncio.run(update_database()))

def main():
    app = ApplicationBuilder() 
        .read_timeout(100) 
        .write_timeout(100) 
        .token(os.environ["BOT_TOKEN"]) 
        .build()

    app.add_handler(CommandHandler("start", start_bot))
    app.add_handler(CommandHandler("prossima", next_match_command))
    app.add_handler(CommandHandler("prossime10", next_10_matches_command))
    app.add_handler(CommandHandler("calendario", get_all_command))

    app.run_polling()
    schedule.run_pending()

if __name__ == "__main__":
    main()

The Bot generally works, except that to save the data in the DB I have to close the application every time. I have difficulty doing this manually though; also, I have problems as described above with messages in channels.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật