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)
apihelper.proxy = {'https':'socks5://userproxy:password@proxy_address:port'}
apihelper.proxy = {'https':'socks5h://userproxy:password@proxy_address:port'}
if __name__ == '__main__':
bot.polling(none_stop=True)
WEBHOOK_HOST = 'URL' #без http:// и https://
WEBHOOK_PORT = 88 # 443, 80, 88 or 8443 (port need to be 'open')
WEBHOOK_LISTEN = '0.0.0.0' # In some VPS you may need to put here the IP addr
WEBHOOK_SSL_CERT = '/etc/nginx/ssl/FILE.cer' # Path to the ssl certificate
WEBHOOK_SSL_PRIV = '/etc/nginx/ssl/FILE.key' # Path to the ssl private key
WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (constant.token)
bot = telebot.TeleBot(constant.token)
class WebhookServer(object):
@cherrypy.expose
def index(self):
if 'content-length' in cherrypy.request.headers and \
'content-type' in cherrypy.request.headers and \
cherrypy.request.headers['content-type'] == 'application/json':
length = int(cherrypy.request.headers['content-length'])
json_string = cherrypy.request.body.read(length).decode("utf-8")
update = telebot.types.Update.de_json(json_string)
# Эта функция обеспечивает проверку входящего сообщения
bot.process_new_updates([update])
return ''
else:
raise cherrypy.HTTPError(403)
bot.remove_webhook()
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH,
certificate=open(WEBHOOK_SSL_CERT, 'r'))
cherrypy.config.update({
'server.socket_host': WEBHOOK_LISTEN,
'server.socket_port': WEBHOOK_PORT,
'server.ssl_module': 'builtin',
'server.ssl_certificate': WEBHOOK_SSL_CERT,
'server.ssl_private_key': WEBHOOK_SSL_PRIV
})
cherrypy.quickstart(WebhookServer(), WEBHOOK_URL_PATH, {'/': {}})
def log(message):
print("<!------!>")
from datetime import datetime
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=['start'])
def cmd_start(message):
bot.send_message(message.from_user.id, 'Привет')
log(message)
@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 = 'path/files/' + 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(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/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)
log(message)
@bot.message_handler(commands=['start'])
def hadle_text(message):
user_markup = telebot.types.ReplyKeyboardMarkup(True, False)
button_phone = types.KeyboardButton(text="", request_contact=True)
user_markup.add(button_phone)
send = bot.send_message(message.chat.id, 'Поделитесь своим номером',
reply_markup=user_markup)
bot.register_next_step_handler(send, hello)
log(message)