from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
vk_session = vk_api.VkApi(token=constant.token)
vk = vk_session.get_api()
longpoll = VkBotLongPoll(vk_session, constant.group_id)
# Обработка событий ВК
for event in longpoll.listen():
# Начало беседы
if event.obj.text == 'Начать':
...
# Ограничение доступа к боту по ID
@bot.message_handler(func=lambda message: message.chat.id not in users)
def some(message):
bot.send_message(message.chat.id, 'Не дозволено общаться с незнакомцами')
# Ограничение выполнение команды start
@bot.message_handler(func=lambda message: message.chat.id not in users, commands=['start'])
def some(message):
bot.send_message(message.chat.id, 'Не дозволено')
# Ограничение выполнение команд
@bot.message_handler(func=lambda message: message.chat.id not in users_command,
commands=['command1', 'command2', 'command3', 'command4'])
def some(message):
bot.send_message(message.chat.id, 'Не дозволено')
@bot.message_handler(commands=['emailcreate'])
def email_create_request_domain(message):
if message.text.lower() == 'отмена':
bot.send_message(message.from_user.id, 'На нетъ и суда нетъ')
return
send = bot.send_message(message.chat.id, 'Зьдати емейлъ\nПочтовый ящикъ:\n'
'example.ru, example1.ru, example2.ru, example3.ru, example4.ru ')
bot.register_next_step_handler(send, email_create_request_data)
def email_create_request_data(message):
global domain_uis
domain_uis = message.text.lower()
if message.text.lower() == 'отмена':
bot.send_message(message.from_user.id, 'На нетъ и суда нетъ')
return
send = bot.send_message(message.chat.id, 'Введите: ФИО должность нумеръ')
bot.register_next_step_handler(send, email_create)
def email_create(message):
first = message.text.split()[1]
last = message.text.split()[0]
middle = message.text.split()[2]
phone = message.text.split()[4]
position = message.text.split()[3]
uis_login = create_nickname(first, last, middle, 'email_create')
bot.send_message(message.chat.id, '✅ логинъ')
uis_password = create_password()
bot.send_message(message.chat.id, '✅ шифръ')
id_yc = create_email(first, last, middle, phone, position, uis_password, uis_login, domain_uis)
if 'Response' in str(id_yc):
bot.send_message(message.chat.id, '❌ емейлъ' + '\n' + id_yc)
return
else:
bot.send_message(message.chat.id, '✅ емейлъ')
bot.send_message(message.chat.id, uis_login + '@' + domain_uis + '\n' + uis_password)
@bot.message_handler(commands=['start'])
def cmd_start(message):
start_keyboard = types.InlineKeyboardMarkup()
start_service = types.InlineKeyboardButton(text='Службы', callback_data='service')
start_system = types.InlineKeyboardButton(text='Система', callback_data='system')
start_check = types.InlineKeyboardButton(text='Чек', callback_data='check')
start_other = types.InlineKeyboardButton(text='Другое', callback_data='other')
start_keyboard.add(start_service, start_system)
start_keyboard.add(start_check, start_other)
bot.send_message(message.chat.id, '', reply_markup=start_keyboard)
start = telebot.types.ReplyKeyboardMarkup(True, False)
start.row('Wunderlist')
start.row('Telegraph')
start.row('Погода')
start.row('Контакты')
bot.send_message(message.from_user.id, 'Выбери сервис', reply_markup=start)
def log(message):
print("<!------!>")
print(datetime.now())
print("Сообщение от {0} {1} (id = {2}) \n {3}".format(message.from_user.first_name,
message.from_user.last_name,
str(message.from_user.id), message.text))
@bot.message_handler(commands=['create'])
def cmd_create(message):
send = bot.send_message(message.chat.id, 'Введи название задачи')
bot.register_next_step_handler(send, create)
log(message)
@bot.message_handler(commands=['subscribe'])
def send_welcome(message):
global sub
sub = [line.rstrip('\n') for line in open(subscribefile, 'rt')]
if str(message.text) not in sub:
with open(subscribefile, 'a') as f:
f.write(str(message.text) + "\n")
sub = [line.rstrip('\n') for line in open(subscribefile, 'rt')]
bot.send_message(message.chat.id, "чат айди записан")
@bot.message_handler(content_types=['document'])
def handle_file(message):
try:
chat_id = message.chat.id
file_info = bot.get_file(message.document.file_id)
downloaded_file = bot.download_file(file_info.file_path)
src = 'C:/Python/Project/tg_bot/files/received/' + message.document.file_name;
with open(src, 'wb') as new_file:
new_file.write(downloaded_file)
bot.reply_to(message, "Пожалуй, я сохраню это")
except Exception as e:
bot.reply_to(message, e
@bot.message_handler(commands=['uiscreate'])
def uis_request_domain(message):
if message.text.lower() == 'отмена':
bot.send_message(message.from_user.id, 'Выполнение команды отменено')
return
send = bot.send_message(message.chat.id, 'Создание пользователя\nВведите почтовый домен:\n'
'domain.ru')
bot.register_next_step_handler(send, uis_request_data)
def uis_request_data(message):
global domain_uis
domain_uis = message.text.lower()
if message.text.lower() == 'отмена':
bot.send_message(message.from_user.id, 'Выполнение команды отменено')
return
send = bot.send_message(message.chat.id, 'Введите: ФИО должность телефон')
bot.register_next_step_handler(send, create_uis)
def create_uis(message):
first = message.text.split()[1]
last = message.text.split()[0]
middle = message.text.split()[2]
phone = message.text.split()[4]
position = message.text.split()[3]
<...>
import telebot
from telebot import apihelper
bot = telebot.TeleBot(TOKEN)
apihelper.proxy = {'https': 'socks5h://user:password@IP:1080'}
<...>
If you want to use socket5 proxy you need install dependency pip install requests[socks] and make sure, that you have the latest version of gunicorn, PySocks, pyTelegramBotAPI, requests and urllib3
@bot.message_handler(commands=['uiscreate'])
def uis_request_domain(message):
if message.text.lower() == 'отмена':
bot.send_message(message.from_user.id, 'Выполнение команды отменено')
return
send = bot.send_message(message.chat.id, 'Создание пользователя\nВведите почтовый домен:\n'
'domain.ru')
bot.register_next_step_handler(send, uis_request_data)
def uis_request_data(message):
global domain_uis
domain_uis = message.text.lower()
if message.text.lower() == 'отмена':
bot.send_message(message.from_user.id, 'Выполнение команды отменено')
return
send = bot.send_message(message.chat.id, 'Введите: ФИО должность телефон')
bot.register_next_step_handler(send, create_uis)
def create_uis(message):
last = message.text.split()[0]
first = message.text.split()[1]
middle = message.text.split()[2]
position = message.text.split()[3]
phone = message.text.split()[4]
<...>
Inline keyboard
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
if call.data == "comproxy":
comproxy_keyboard = types.InlineKeyboardMarkup()
call_button_comproxy_restart = types.InlineKeyboardButton(text="Restart", callback_data="comproxy_restart")
call_button_comproxy_status = types.InlineKeyboardButton(text="Status", callback_data="comproxy_status")
call_button_comproxy_back = types.InlineKeyboardButton(text="Назад", callback_data="back")
comproxy_keyboard.add(call_button_comproxy_restart, call_button_comproxy_status)
comproxy_keyboard.add(call_button_comproxy_back)
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Выберите:",
reply_markup=comproxy_keyboard)
###
elif call.data == "back":
back_keyboard = types.InlineKeyboardMarkup()
call_button_back_comproxy = types.InlineKeyboardButton(text="Comproxy", callback_data="comproxy")
call_button_back_ser2net = types.InlineKeyboardButton(text="Ser2net", callback_data="ser2net")
call_button_back_cups = types.InlineKeyboardButton(text="Cups", callback_data="cups")
back_keyboard.add(call_button_back_comproxy, call_button_back_ser2net)
back_keyboard.add(call_button_back_cups)
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
text='Чего изволите, господин?', reply_markup=back_keyboard)
@bot.message_handler(commands=['start'])
def cmd_start(message):
bot.send_message(message.from_user.id, 'Привет')
https://api.telegram.org/botTOKEN/sendMessage?chat_id=CHAT_ID&text=TEXT