Skip to content

Instantly share code, notes, and snippets.

@Denver-sn
Created December 7, 2021 00:16
Show Gist options
  • Save Denver-sn/76fded15863e42456a2979ea53410ca4 to your computer and use it in GitHub Desktop.
Save Denver-sn/76fded15863e42456a2979ea53410ca4 to your computer and use it in GitHub Desktop.
Add premium / Auto remove premium Telegram Bot with PyTelegramBotAPI
# > Library
import telebot, schedule
from datetime import datetime, date
import time, sqlite3
import threading
bot = telebot.TeleBot("YOUR_TOKEN")
# > DATABASE CONNEXION
conn = sqlite3.connect('sample_database.db', check_same_thread=False)
# > AUTO CHECK PREMIUM
def auto_check():
today_date = date.today()
currentDateTime = datetime.strptime(str(today_date), '%Y-%m-%d')
# DATABASE STUCTURE
# record[0] = id
# record[1] = telegram id
# record [2] = is_premium value = "yes" or "no"
# record[3] = is_admin value = "yes" or "no"
# record[4] = premium_expire value = Date Eg: 2021-12-31
cursor = conn.cursor()
cursor.execute(
f"SELECT * from user_table")
records = cursor.fetchall()
for record in records:
try:
expire = datetime.strptime(record[4], "%Y-%m-%d")
if currentDateTime < expire:
continue
elif currentDateTime >= expire:
# Removing Premium
text = 'no'
update_data = """Update user_table set is_premium = ? where user_id = ?"""
columnValues = (text, record[1])
cursor = conn.cursor()
cursor.execute(update_data, columnValues)
conn.commit()
cursor.close()
# Removing Time
currentDateTime2 = date.today()
cursor = conn.cursor()
update_data = """Update user_table set premium_expire = ? where user_id = ?"""
columnValues = (currentDateTime2, record[1])
cursor.execute(update_data, columnValues)
conn.commit()
cursor.close()
except Exception as e:
print(f"ERROR [AUTO CHECK FOR USER {record[1]}] on line 47 | | {e}")
# > RUN THE AUTO CHECKER EACH SECOND
schedule.every(1).seconds.do(auto_check)
def run_schedule():
while True:
schedule.run_pending()
time.sleep(1)
th_2 = threading.Thread(target = run_schedule)
th_2.start()
# ------------- ADD PREMIUM COMMAND ---------------------
# PREMIUM ACTIVATION
@bot.message_handler(commands=['activate'])
def stat(message):
chat_id = message.chat.id
cursor = conn.cursor()
cursor.execute(
f"SELECT * from user_table WHERE user_id = {chat_id}")
records = cursor.fetchone()
is_admin = records[3]
if is_admin == "yes":
send = bot.reply_to(message, f"Send me user id|days:")
bot.register_next_step_handler(send, receive_a)
else:
bot.reply_to(message, f"You're not an admin")
def receive_a(message):
text = message.text
try:
split_text = text.split('|')
user_id = split_text[0]
days_ = int(split_text[1])
cursor = conn.cursor()
cursor.execute(
f"SELECT * from user_table WHERE user_id = {user_id}")
records = cursor.fetchone()
is_premium = records[2]
cursor.close()
if is_premium == 'yes':
bot.send_message(message.chat.id, f"User have already an Premium")
else:
# Adding Premium
text = 'yes'
update_data = """Update user_table set is_premium = ? where user_id = ?"""
columnValues = (text, user_id)
cursor = conn.cursor()
cursor.execute(update_data, columnValues)
conn.commit()
cursor.close()
# Adding exipre time
from datetime import timedelta
currentDateTime2 = date.today()+timedelta(days=days_)
cursor = conn.cursor()
update_data = """Update user_table set premium_expire = ? where user_id = ?"""
columnValues = (currentDateTime2, user_id)
cursor.execute(update_data, columnValues)
conn.commit()
cursor.close()
try:
bot.send_message(user_id, f"Hello, we're happy to announce you that your premium has been activated! and will expire the: <b>{currentDateTime2}</b>", parse_mode='HTML')
bot.send_message(message.chat.id, f"Premium activated and will expire the: <b>{currentDateTime2}</b>", parse_mode='HTML')
bot.send_message(-1001529075266, f"<b>PREMIUM PURCHASE</b>\n\nAn premium has been activated by this <a href='tg://user?id={message.chat.id}'>admin</a> for <a href='tg://user?id={user_id}'>{user_id}</a>\n\nPremium Expire: <b>{currentDateTime2}</b>", parse_mode='HTML')
except:
bot.send_message(message.chat.id, f"Premium activated but message not sent to the user")
bot.send_message(-1001529075266, f"<b>PREMIUM PURCHASE</b>\n\nAn premium has been activated by this <a href='tg://user?id={message.chat.id}'>admin</a> for <a href='tg://user?id={user_id}'>{user_id}</a>\n\nPremium Expire: <b>{currentDateTime2}</b>", parse_mode='HTML')
except Exception as e:
bot.send_message(-1001529075266, f"ERROR [PREMIUM ACTIVATION] on line 414 | {e}")
bot.send_message(message.chat.id, f"Error while activing the premium")
bot.polling(none_stop=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment