Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # -*- coding: utf-8 -*-
- import logging
- import io
- import os
- import time
- from telegram import Emoji, ForceReply, ReplyKeyboardMarkup, KeyboardButton
- from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
- logging.basicConfig(format='[%(asctime)s] %(message)s',
- level=logging.INFO)
- ru_str = 'ё1234567890йцукенгшщзхъ\\фывапролджэячсмитьбю.Ё!"№;%:?*()ЙЦУКЕНГШЩЗХЪ/ФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,'
- en_str = '`1234567890qwertyuiop[]\\asdfghjkl;\'zxcvbnm,./~!@#$%^&*()QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?'
- def transform_ruen(s):
- ans = []
- for i in s:
- if i in ru_str:
- ans.append(en_str[ru_str.index(i)])
- else:
- ans.append(i)
- return ''.join(ans)
- my_id = 214334796
- channel_id = '@best_captcha'
- captcha_state = None
- code_sent = False
- first_time = 0
- last_r = None
- def sendPhoto():
- global captcha_state, code_sent, first_time, last_r
- f = open('/var/www/vkbot/accounts/sasha/captcha.txt').read().split() + ['']
- if not os.path.isfile('/var/www/vkbot/accounts/sasha/captcha.png') or f[0] != 'sid':
- if first_time:
- logging.info('Disappeared too fast')
- first_time = 0
- if captcha_state and not code_sent:
- if len(f) == 1:
- updater.bot.sendMessage(chat_id=my_id, text="Слишком поздно")
- logging.info('Too late')
- else:
- updater.bot.sendMessage(chat_id=my_id, text="Уже не надо")
- logging.info('Not needed')
- if f[0] == 'cor':
- try:
- updater.bot.sendPhoto(chat_id='@best_captcha', photo=last_r, disable_notification=True, caption=f[1])
- except Exception as e:
- logging.exception(e)
- code_sent = False
- captcha_state = None
- return
- if f[1] == captcha_state:
- return
- if not first_time:
- first_time = time.time()
- return
- if first_time + 6 > time.time():
- return
- captcha_state = f[1]
- r = io.BufferedReader(io.BytesIO(open('/var/www/vkbot/accounts/sasha/captcha.png', 'rb').read()))
- updater.bot.sendPhoto(chat_id=my_id, photo=r)
- last_r = io.BufferedReader(io.BytesIO(open('/var/www/vkbot/accounts/sasha/captcha.png', 'rb').read()))
- logging.info('Sending a picture')
- first_time = 0
- # Example handler. Will be called on the /set command and on regular messages
- def set_value(bot, update):
- chat_id = update.message.chat_id
- if chat_id != my_id:
- logging.info('Message from unknown user')
- return
- text = transform_ruen(update.message.text.lower().strip())
- if text.isalnum() and 4 <= len(text) <= 8:
- global code_sent
- code_sent = True
- logging.info('Writing ' + text)
- with open('/var/www/vkbot/accounts/sasha/captcha.txt', 'w') as f:
- f.write('key ' + text.lower().strip())
- else:
- logging.info('Bad message')
- bot.sendMessage(chat_id=my_id, text="Не верю")
- # Create the Updater and pass it your bot's token.
- updater = Updater("...")
- updater.dispatcher.add_handler(MessageHandler([Filters.text], set_value))
- # Start the Bot
- updater.start_polling()
- logging.info('Bot started')
- # Run the bot until the user presses Ctrl-C or the process receives SIGINT,
- # SIGTERM or SIGABRT
- while True:
- sendPhoto()
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement