Skip to content

Instantly share code, notes, and snippets.

@sacsbrainz
Forked from Denver-sn/example_bot_2.py
Last active September 25, 2021 00:11
Show Gist options
  • Save sacsbrainz/85be76bc986f8ee67bea74baade4eabe to your computer and use it in GitHub Desktop.
Save sacsbrainz/85be76bc986f8ee67bea74baade4eabe to your computer and use it in GitHub Desktop.
import telebot
import sqlite3
import time
# DATABASE CONFIGURATION
conn = sqlite3.connect('sacs_db.db', check_same_thread=False)
cursor = conn.cursor()
def db_wallet(user_id: int, wallet_a: str):
cursor.execute('INSERT INTO user_db (user_id, wallet) VALUES (?, ?)',
(user_id, wallet_a))
conn.commit()
#---------------------------#
TOKEN = "2022034002:AAHZzCYZG794XtPfpZQzELwRsoyA9mg88Ys"
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(commands=['start'])
def start(message):
user = message.chat.id
name = message.chat.first_name
try:
cursor.execute(f"SELECT * from user_db WHERE user_id = {user}")
records = cursor.fetchone()
data_id = records[0]
wallet_in_db = records[2]
bot.send_message(message.chat.id, f"Hello {name}, welcome back!")
except Exception as e:
print(e)
try:
db_wallet(user_id=user, wallet_a=None)
bot.send_message(message.chat.id, f"Hello {name}, welcome New user!")
except Exception as e:
print(e)
@bot.message_handler(commands=['wallet'])
def start(message):
user = message.chat.id
name = message.chat.first_name
if message.chat.type == 'private':
try:
cursor.execute(f"SELECT * from user_db WHERE user_id = {user}")
records = cursor.fetchone()
data_id = records[0]
wallet_in_db = records[2]
if wallet_in_db == None:
bot.send_message(message.chat.id, f"Sorry! you didn't set any wallet :/")
else:
bot.send_message(message.chat.id, f"Your Wallet is : {wallet_in_db}")
except:
bot.send_message(message.chat.id, f"Error! seems you're not in our database")
@bot.message_handler(commands=['set_wallet'])
def start(message):
sent = bot.reply_to(message, f"Please send your Wallet Adress: ")
bot.register_next_step_handler(sent, receive)
def receive(message):
wallet = message.text
user = message.chat.id
name = message.chat.first_name
try:
cursor.execute(f"SELECT * from user_db WHERE user_id = {user}")
records = cursor.fetchone()
data_id = records[0]
wallet_in_db = records[2]
print(wallet_in_db)
db_wallet(user_id=user, wallet_a=wallet)
bot.send_message(message.chat.id, f"Wallet Updated !")
except Exception as e:
print(e)
db_wallet(user_id=user, wallet_a=wallet)
bot.send_message(message.chat.id, f"New Wallet Adress added")
while True:
try:
bot.polling(none_stop=True)
except Exception:
time.sleep(60)
CREATE TABLE user_db (
id PRIMARY KEY
UNIQUE,
user_id INTEGER UNIQUE ON CONFLICT REPLACE,
wallet INTEGER UNIQUE
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment