Python scheduler and Telegram issues

203
July 21, 2018, at 9:00 PM

So, here's a piece of my code. I tried to remove as much other information as I could.

Firstly, I want to ask a question and if a user answers yes, his FirstName, LastName and ID go to the database. Then, I want to choose a winner every day. The issue is when I use this

while 1:
    schedule.run_pending()
    time.sleep(1)

My message_handler stops working. It just doesn' respond to messages.

import telebot
import sqlite3
import schedule
import time

token = "<token here>"
bot = telebot.TeleBot(token)
connection = sqlite3.connect('Participants.db', check_same_thread=False)

def create_table():
    cursor = connection.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS participants(fname TEXT, lname TEXT, chat_id INTEGER UNIQUE )")
    cursor.close()

create_table()
def dynamic_data_entry(FirstName, LastName, ChatID):
    cursor = connection.cursor()
    fname = FirstName
    lname = LastName
    chat_id = ChatID
    cursor.execute("INSERT OR IGNORE INTO participants (fname, lname, chat_id) VALUES (?, ?, ?)", (fname,lname, chat_id))
    connection.commit()
    cursor.close()

def choose_winner():
    cursor = connection.cursor()
    cursor.execute("SELECT DISTINCT * FROM participants ORDER BY RANDOM() LIMIT 1")
    data = cursor.fetchall()
    choose_winner.FirstFNAME = data[0][0]
    choose_winner.FirstLNAME = data[0][1]
    choose_winner.FirstID = data[0][2]
    connection.commit()
    cursor.close()

def send_winner(FirstFname, FirstLname):
    firstF = FirstFname
    firstL = FirstLname
    bot.send_message(##ID would be here##, "The winner is: " + str(firstF) + " " + str(firstL))

@bot.message_handler(content_types=['text'])
def handle_text(message):
    if message.text == 'Yes':
        bot.send_message(message.from_user.id, 'You answered yes')
        dynamic_data_entry(message.from_user.first_name, message.from_user.last_name, message.from_user.id)
    elif message.text == 'No':
        bot.send_message(message.from_user.id, 'You answered no')
    else:
        bot.send_message(message.from_user.id, 'ERROR')

choose_winner()
schedule.every(5).seconds.do(lambda: send_winner(choose_winner.FirstFNAME, choose_winner.FirstLNAME))
while 1:
    schedule.run_pending()
    time.sleep(1)
bot.polling(none_stop = True, interval = 0)
READ ALSO
Change cmd prompt size in Python

Change cmd prompt size in Python

Hey guys I'm making a text-based CMD application in Python

243
Python C++ extensions with CMake and SetupTools?

Python C++ extensions with CMake and SetupTools?

I have a C++ project that provides a Python wrapperI'm building that project using CMake and I would like to have a setup

493
Change label text while in background loop

Change label text while in background loop

The following code will run a window that displays your public ip in a labelIt's running a thread in the background that will call check_ip_change every 5 seconds

155
pygame - get current color of text (pygame.Surface type)

pygame - get current color of text (pygame.Surface type)

I want to randomize colors and change color for text every few seconds, so I want to make sure that I dont use the same colorHow can I know the current color of text?

181