async def delete_a_bot_by_label( bot: BotFind, user: UserDB = Depends(fastapi_users.current_user(verified=True))): label = bot.label bot_list = user.bots bot_labels = list(map(lambda x: x.label, bot_list)) if label in bot_labels: idx = bot_labels.index(label) bot = bot_list[idx] if os.getenv("HOST_IP") is not None: telegram_bot = TeleBot(bot.token) telegram_bot.remove_webhook() bot_list.remove(bot) bot_list = list(map(lambda x: dict(x), bot_list)) await user_db.update_one({"id": user.id}, {"$set": { "bots": bot_list, }}) return bot_list else: raise HTTPException(status_code=404, detail="The requested bot does not exist")
async def adds_a_bot(bot: Bot, user: UserDB = Depends( fastapi_users.current_user(verified=True))): bot_list = user.bots if len(bot_list) < 5: if bot in bot_list: raise HTTPException(status_code=409, detail="The requested bot already exists") else: if os.getenv("HOST_IP") is not None: telegram_bot = TeleBot(bot.token) telegram_bot.remove_webhook() url = urllib.parse.urljoin( os.getenv("URL"), f"/bots/webhook/{user.id}/{bot.token}") telegram_bot.set_webhook(url) bot_list = list(map(lambda x: dict(x), bot_list)) bot_list.append(bot.dict()) await user_db.update_one({"id": user.id}, {"$set": { "bots": bot_list, }}) return bot_list else: raise HTTPException(status_code=403, detail="You cannot have more than 5 bots")
def start(config: Config): bot = TeleBot(config.TELE_TOKEN) Handler( bot=bot, issuer_id=config.ISSUER_ID, processor=Processor( parser=get_parser(config.DIR_PATTERN), db_man=DBManager[Cowboy]( path=get_data_folder_path(), object_type=Cowboy, create_if_missing=True, ), explorer_cls=get_explorer_cls(config.GITHUB_TOKEN), ), serializer=Serializer(), deserializer=Deserializer(), ) DEF_PORT = "8443" URL_BASE = f"https://{config.HOST}" URL_PATH = f"/{config.TELE_TOKEN}" cert = f"{get_data_folder_path()}/{config.CERT}" bot.remove_webhook() if config.DEBUG != "0": logger.info("Running in a DEBUG mode.") bot.polling(none_stop=True) else: logger.info("Running in a PRODUCTION mode.") flask_app = get_flask_app(bot) bot.set_webhook(url=URL_BASE + URL_PATH, certificate=open(cert, "rb")) flask_app.run(host=config.LISTEN, port=DEF_PORT)
def post_fork(server, worker): if forked == workers: from time import sleep from telebot import TeleBot from config import BOT_TOKEN from time import sleep from scheduler_controller import schedule_controller from logger_settings import hr_logger hr_logger.info('Сервіс запущено') bot = TeleBot(BOT_TOKEN, threaded=False) bot.remove_webhook() sleep(1) bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH, certificate=open(WEBHOOK_SSL_CERT, 'r')) schedule_controller()
def main_definition(): """Определение управляющего бота, регистрации, автоответчика""" try: configs = functions.load_data('data/config-bot.json') bot_token, my_id = configs['TOKEN'], configs['my_ID'] except FileNotFoundError as error: message = "\n - Файл с конфигурациями для бота не найден! Зарегестрируйте данные!" print(message + "\n") functions.logging(message, error) functions.do_command() else: bot = TeleBot(bot_token) bot.remove_webhook() registration = Register() answering_bot = AnsweringBot() if not os.path.exists(registration.LOGS): functions.creating_file(registration.LOGS) return bot, registration, answering_bot, my_id
return '' else: flask.abort(403) @webapp.route('/sendMessage/', methods=['GET']) def echo_message(): sid = flask.request.args.get('id') text = flask.request.args.get('text') return send_message(dict(sid=sid, text=text)) def send_message(data): userList = tdb.find_ship_user(dict(sid=data['sid'])) result_text = {} for i, cid in enumerate([x['uid'] for x in userList]): status = bot.send_message(chat_id=cid, text=data['text']) result_text[i] = status.json return result_text bot.remove_webhook() sleep(1) bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH, certificate=open(WEBHOOK_SSL_CERT, 'r')) webapp.run(host=WEBHOOK_LISTEN, port=WEBHOOK_PORT, ssl_context=(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV), debug=False)
if 'PRODUCTION' in os.environ: sslify = SSLify(app) app.config.from_object(Config) db = SQLAlchemy(app) migrate = Migrate(app, db) login = LoginManager(app) login.login_view = 'auth.login' login.login_message = 'Для входа в систему необходима регистрация.' login.login_message_category = 'error' import application.core.models as models @app.shell_context_processor def shell_context(): return {'TvChannel': models.TVChannel} from application.bot import bp as bot_bp app.register_blueprint(bot_bp) from application.auth import bp as auth_bp app.register_blueprint(auth_bp, url_prefix='/auth') from application.admin import bp as admin_bp app.register_blueprint(admin_bp) from application.utils import filters if 'ADMIN_DEV' not in os.environ and 'PRODUCTION' not in os.environ: logger.setLevel(logging.DEBUG) telegram_bot.remove_webhook() telegram_bot.polling(none_stop=True)
class TelegramBot: def __init__(self, work_dir: Path, token: str): self.last_requests = {} # type: Dict[int, List[datetime]] self.work_dir = work_dir self.work_dir.mkdir(0o755, parents=True, exist_ok=True) self.token = token self.bot = TeleBot(token) self.bot.add_message_handler({ 'function': self.process_link, 'filters': { 'regexp': r'https?:\/\/.+\..+' } }) self.bot.add_message_handler({ 'function': self.process_message, 'filters': { 'func': lambda _: True } }) def process_link(self, message: Message): user_id = message.from_user.id if user_id not in self.last_requests: self.last_requests[user_id] = [] limit_req, limit_min = get_rate_limit() now = datetime.now(tz=timezone.utc) last_requests = [] for dt in self.last_requests[user_id]: time_passed = now - dt if time_passed.min < timedelta(minutes=limit_min): last_requests.append(dt) self.last_requests[user_id] = last_requests if len(self.last_requests[user_id]) > limit_req: next_available = self.last_requests[user_id][0] + timedelta( minutes=limit_min) - now self.bot.reply_to( message, f'Rate limited. Try again in {next_available.seconds} seconds') return else: self.last_requests[user_id].append(now) msg = self.bot.reply_to(message, "Link detected, processing") # detach from telebot update thread def download(): try: with YoutubeDL({'noplaylist': True}) as ydl: info = ydl.extract_info(message.text, download=False) if info['duration'] > 900: self.bot.edit_message_text('Source too long', message_id=msg.id, chat_id=msg.chat.id) return with YoutubeDL(get_options_audio(self.work_dir)) as ydl: ydl.add_post_processor( UploadHandler(self.bot, msg.id, msg.chat.id)) self.bot.edit_message_text('Downloading', message_id=msg.id, chat_id=msg.chat.id) ydl.download([message.text]) except UnsupportedError: self.bot.edit_message_text('Unsupported URL', message_id=msg.id, chat_id=msg.chat.id) except DownloadError: self.bot.edit_message_text('Download error', message_id=msg.id, chat_id=msg.chat.id) except Exception as e: self.bot.edit_message_text('Unknown error', message_id=msg.id, chat_id=msg.chat.id) logger.error('Unknown error', exc_info=e) reactor.callInThread(lambda: download()) def process_message(self, message: Message): self.bot.reply_to(message, 'Send me a link') def polling(self): self.bot.delete_webhook() self.bot.polling(long_polling_timeout=5) def stop_polling(self): self.bot.stop_polling() def set_webhook(self, host: str, port: int): cert_path, pkey_path = get_or_create_root_cert(self.work_dir, host) self.bot.remove_webhook() self.bot.set_webhook(url=f'https://{host}:{port}/{self.token}/', certificate=open(cert_path, 'r')) bot = self.bot class WebhookHandler(Resource): isLeaf = True def render_POST(self, request): request_body_dict = json.load(request.content) update = Update.de_json(request_body_dict) reactor.callInThread(lambda: bot.process_new_updates([update])) return b'' root = ErrorPage(403, 'Forbidden', '') root.putChild(self.token.encode(), WebhookHandler()) site = Site(root) sslcontext = ssl.DefaultOpenSSLContextFactory(str(pkey_path), str(cert_path)) reactor.listenSSL(port, site, sslcontext)
class TelegramBot: def __init__(self): social_app = SocialApp.objects.last() TELEGRAM_TOKEN = social_app.secret TELEGRAM_WEBHOOK_HOST = social_app.sites.last().domain TELEGRAM_WEBHOOK_PATH = reverse("telegrambot:webhook") self.bot = TeleBot(TELEGRAM_TOKEN) self.WEBHOOK_URL = f"https://{TELEGRAM_WEBHOOK_HOST}{TELEGRAM_WEBHOOK_PATH}" logger.debug("%s: __init__()", self.__class__) def send_message(self, chat_id: int, text: str, reply_markup=None): try: self.bot.send_message(chat_id, text, reply_markup=reply_markup) except apihelper.ApiTelegramException as msg: logger.exception(msg) logger.debug(f"{self.__class__}: send_message({chat_id}, {text})") def send_photo(self, chat_id: str, photo_path: str, caption=None, reply_markup=None): with open(photo_path, "rb") as photo_file: photo = photo_file.read() try: self.bot.send_photo(chat_id, photo=photo, caption=caption, reply_markup=reply_markup) except apihelper.ApiTelegramException as msg: logger.exception(msg) logger.info( f"{self.__class__}: User {chat_id} baned your TelegramBot") else: logger.debug( f"{self.__class__}: send_photo({chat_id}, {photo_path})") def answer_callback_query(self, callback_id, text=None, show_alert=None, url=None, cache_time=None): self.bot.answer_callback_query( callback_id, text=text, show_alert=show_alert, url=url, cache_time=cache_time, ) def set_webhook(self): logger.debug("%s: set_hook() %s" % (self.__class__, self.WEBHOOK_URL)) return self.bot.set_webhook(url=self.WEBHOOK_URL) def remove_webhook(self): logger.debug("%s: remove_webhook()", self.__class__) return self.bot.remove_webhook() def get_kb_phone(self): keyboard = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True) button_phone = types.KeyboardButton(text="Отправить номер телефона", request_contact=True) button_geo = types.KeyboardButton(text="Отправить местоположение", request_location=True) keyboard.add(button_phone, button_geo) return keyboard def get_kb_catalog(self): keyboard = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True, one_time_keyboard=True) buttons = [ types.KeyboardButton(text=item.name) for item in models.Catalog.objects.all() ] keyboard.add(*buttons) return keyboard def get_kb_inline_buy(self, product): keyboard = types.InlineKeyboardMarkup() buy = _("Buy") text = f"{buy}: {product.name}" buy_btn = types.InlineKeyboardButton(text, callback_data=str(product.id)) keyboard.add(buy_btn) return keyboard