Advertisement
Guest User

Untitled

a guest
Sep 11th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 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. captcha_state = None
  29. code_sent = False
  30. first_time = 0
  31.  
  32. def sendPhoto():
  33.     global captcha_state, code_sent, first_time
  34.     f = open('/var/www/vkbot/accounts/sasha/captcha.txt').read().split() + ['']
  35.     if not os.path.isfile('/var/www/vkbot/accounts/sasha/captcha.png') or f[0] != 'sid':
  36.         if first_time:
  37.             logging.info('Disappeared too fast')
  38.         first_time = 0
  39.         if captcha_state and not code_sent:
  40.             if len(f) == 1:
  41.                 updater.bot.sendMessage(chat_id=my_id, text="Слишком поздно")
  42.                 logging.info('Too late')
  43.             else:
  44.                 updater.bot.sendMessage(chat_id=my_id, text="Уже не надо")
  45.                 logging.info('Not needed')
  46.         code_sent = False
  47.         captcha_state = None
  48.         return
  49.     if f[1] == captcha_state:
  50.         return
  51.     if not first_time:
  52.         first_time = time.time()
  53.         return
  54.     if first_time + 6 > time.time():
  55.         return
  56.     captcha_state = f[1]
  57.     r = io.BufferedReader(io.BytesIO(open('/var/www/vkbot/accounts/sasha/captcha.png', 'rb').read()))
  58.     updater.bot.sendPhoto(chat_id=my_id, photo=r)
  59.     logging.info('Sending a picture')
  60.     first_time = 0
  61.  
  62. # Example handler. Will be called on the /set command and on regular messages
  63. def set_value(bot, update):
  64.     chat_id = update.message.chat_id
  65.     if chat_id != my_id:
  66.         logging.info('Message from unknown user')
  67.         return
  68.     text = transform_ruen(update.message.text.lower().strip())
  69.     if text.isalnum() and 4 <= len(text) <= 8:
  70.         global code_sent
  71.         code_sent = True
  72.         logging.info('Writing ' + text)
  73.         with open('/var/www/vkbot/accounts/sasha/captcha.txt', 'w') as f:
  74.             f.write('key ' + text.lower().strip())
  75.     else:
  76.         logging.info('Bad message')
  77.         bot.sendMessage(chat_id=my_id, text="Не верю")
  78.  
  79.  
  80. # Create the Updater and pass it your bot's token.
  81. updater = Updater("tut token")
  82.  
  83. updater.dispatcher.add_handler(MessageHandler([Filters.text], set_value))
  84.  
  85. # Start the Bot
  86. updater.start_polling()
  87. logging.info('Bot started')
  88.  
  89. # Run the bot until the user presses Ctrl-C or the process receives SIGINT,
  90. # SIGTERM or SIGABRT
  91. while True:
  92.     sendPhoto()
  93.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement