Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. import logging
  5. import io
  6. import os
  7. import time
  8. from telegram import Emoji, ForceReply, ReplyKeyboardMarkup, KeyboardButton
  9. from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
  10.  
  11. logging.basicConfig(format='[%(asctime)s] %(message)s',
  12.                     level=logging.INFO)
  13.  
  14. ru_str = 'ё1234567890йцукенгшщзхъ\\фывапролджэячсмитьбю.Ё!"№;%:?*()ЙЦУКЕНГШЩЗХЪ/ФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,'
  15. en_str = '`1234567890qwertyuiop[]\\asdfghjkl;\'zxcvbnm,./~!@#$%^&*()QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?'
  16.  
  17. def transform_ruen(s):
  18.     ans = []
  19.     for i in s:
  20.         if i in ru_str:
  21.             ans.append(en_str[ru_str.index(i)])
  22.         else:
  23.             ans.append(i)
  24.     return ''.join(ans)
  25.  
  26.  
  27. my_id = 214334796
  28. channel_id = '@best_captcha'
  29. captcha_state = None
  30. code_sent = False
  31. first_time = 0
  32. last_r = None
  33.  
  34. def sendPhoto():
  35.     global captcha_state, code_sent, first_time, last_r
  36.     f = open('/var/www/vkbot/accounts/sasha/captcha.txt').read().split() + ['']
  37.     if not os.path.isfile('/var/www/vkbot/accounts/sasha/captcha.png') or f[0] != 'sid':
  38.         if first_time:
  39.             logging.info('Disappeared too fast')
  40.         first_time = 0
  41.         if captcha_state and not code_sent:
  42.             if len(f) == 1:
  43.                 updater.bot.sendMessage(chat_id=my_id, text="Слишком поздно")
  44.                 logging.info('Too late')
  45.             else:
  46.                 updater.bot.sendMessage(chat_id=my_id, text="Уже не надо")
  47.                 logging.info('Not needed')
  48.         if f[0] == 'cor':
  49.             try:
  50.                 updater.bot.sendPhoto(chat_id='@best_captcha', photo=last_r, disable_notification=True, caption=f[1])
  51.             except Exception as e:
  52.                 logging.exception(e)
  53.         code_sent = False
  54.         captcha_state = None
  55.         return
  56.     if f[1] == captcha_state:
  57.         return
  58.     if not first_time:
  59.         first_time = time.time()
  60.         return
  61.     if first_time + 6 > time.time():
  62.         return
  63.     captcha_state = f[1]
  64.     r = io.BufferedReader(io.BytesIO(open('/var/www/vkbot/accounts/sasha/captcha.png', 'rb').read()))
  65.     updater.bot.sendPhoto(chat_id=my_id, photo=r)
  66.     last_r = io.BufferedReader(io.BytesIO(open('/var/www/vkbot/accounts/sasha/captcha.png', 'rb').read()))
  67.     logging.info('Sending a picture')
  68.     first_time = 0
  69.  
  70. # Example handler. Will be called on the /set command and on regular messages
  71. def set_value(bot, update):
  72.     chat_id = update.message.chat_id
  73.     if chat_id != my_id:
  74.         logging.info('Message from unknown user')
  75.         return
  76.     text = transform_ruen(update.message.text.lower().strip())
  77.     if text.isalnum() and 4 <= len(text) <= 8:
  78.         global code_sent
  79.         code_sent = True
  80.         logging.info('Writing ' + text)
  81.         with open('/var/www/vkbot/accounts/sasha/captcha.txt', 'w') as f:
  82.             f.write('key ' + text.lower().strip())
  83.     else:
  84.         logging.info('Bad message')
  85.         bot.sendMessage(chat_id=my_id, text="Не верю")
  86.  
  87.  
  88. # Create the Updater and pass it your bot's token.
  89. updater = Updater("...")
  90.  
  91. updater.dispatcher.add_handler(MessageHandler([Filters.text], set_value))
  92.  
  93. # Start the Bot
  94. updater.start_polling()
  95. logging.info('Bot started')
  96.  
  97. # Run the bot until the user presses Ctrl-C or the process receives SIGINT,
  98. # SIGTERM or SIGABRT
  99. while True:
  100.     sendPhoto()
  101.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement