· 5 years ago · Jul 02, 2020, 10:52 PM
1# -*- coding: utf-8 -*-
2"""
3Proxy-bot
4"""
5import telebot
6import config
7import sqlite3
8from telebot import types
9
10bot = telebot.TeleBot(config.TOKEN)
11
12def known_users(uid):
13 global db
14 db = sqlite3.connect('proxies_db.sqlite')
15 cursor = db.cursor()
16
17 cursor.execute('''
18 CREATE TABLE IF NOT EXISTS users (
19 id INTEGER PRIMARY KEY,
20 )
21 ''')
22 cursor.execute("INSERT INTO users (id) VALUES (uid)")
23
24 db.commit()
25
26 db.close()
27
28commands = { # command description used in the "help" command
29 'start' : 'Get used to the bot',
30 'help' : 'Gives you information about the available commands',
31 }
32
33# error handling if user isn't known yet
34# (obsolete once known users are saved to file, because all users
35# had to use the /start command and are therefore known to the bot)
36def get_user_step(uid):
37 if uid in db:
38 return db[uid]
39 else:
40 known_users(uid)
41 db[uid] = 0
42 print("New user detected, who hasn't used \"/start\" yet")
43 return 0
44
45# handle the "/start" command
46@bot.message_handler(commands=['start'])
47def command_start(m):
48 cid = m.chat.id
49 if cid not in db: # if user hasn't used the "/start" command yet:
50 known_users(cid) # save user id, so you could brodcast messages to all users of this bot later
51 db[cid] = 0 # save user id and his current "command level", so he can use the "/getImage" command
52 bot.send_message(cid, "Добро пожаловать, странник, подожди, я тебя сканирую...")
53 bot.send_message(cid, "Сканирование завершено, я знаю тебя?")
54 command_help(m) # show the new user the help page
55 else:
56 command_help(m)
57
58# help page
59@bot.message_handler(command=['help'])
60def command_help(message):
61 # keyboard
62 markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
63 give_me_proxy = types.KeyboardButton("Дай мне прокси")
64 help_info = types.KeyboardButton("Как подключить прокси?")
65
66 markup.add(give_me_proxy, help_info)
67
68 bot.send_message(message.chat.id, "Привет, {0.first_name}!\nЯ - <b>{1.first_name}</b>, бот созданный чтобы отправлять рабочие прокси.".format(message.from_user, bot.get_me()),
69 parse_mode='html', reply_markup=markup)
70
71@bot.message_handler(regexp="Как подключить прокси?")
72def handle_message(message):
73 bot.send_message(message.chat.id, "Чтобы подключиться к прокси нажмите на ссылку которую я отправлю, если нажмешь \"Дай мне прокси\" ",
74 parse_mode='html')
75
76# filter on a specific message
77@bot.message_handler(func=lambda message: message.text == "Дай мне прокси")
78def command_text_hi(m):
79 bot.send_message(m.chat.id, "I love you too!")
80
81# default handler for every other text
82@bot.message_handler(func=lambda message: True, content_types=['text'])
83def command_default(m):
84 command_help(m)
85
86if __name__ == '__main__':
87 bot.infinity_polling()