def __log(self, *args): info = 'entityRecognize_pytorch ' for a in args: info += str(a) log.log(info) if self.trainloger is not None: self.trainloger.info(info)
def initial_bot(use_logging=True, level_name='DEBUG'): bot = telebot.TeleBot(TOKEN) logger = log('bot', 'bot.log', 'INFO') @bot.message_handler(commands=['start']) def start_handler(message: telebot.types.Message): bot.send_message(message.from_user.id, start_mess) logger.info(f"It's start handler. Message from {message.from_user.id}") @bot.message_handler(commands=['help']) def help_handler(message: telebot.types.Message): bot.send_message(message.from_user.id, help_mess) logger.info(f"It's start handler. Message from {message.from_user.id}") @bot.callback_query_handler(func=lambda call: call.data in ['approve']) def callback_handler(call): try: bot.send_message(CHAT, call.message.text) except: pass @bot.message_handler(content_types=['text']) def text_handler(message: telebot.types.Message): keyboard = telebot.types.InlineKeyboardMarkup() keyboard.add(telebot.types.InlineKeyboardButton('Опубликовать', callback_data='approve'), telebot.types.InlineKeyboardButton('Отклонить', callback_data='refuse')) bot.send_message(ADMIN, message.text, reply_markup=keyboard) logger.info(f"It's text handler. Message from {message.from_user.id}") if use_logging: telebot.logger.setLevel(logging.getLevelName(level_name)) return bot
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys from source import config from source.log import log from pathlib import Path CONFIG = {} log = log() write = log.write class Syngrafeas: def __init__(self, modeRun=None): self.modeRun = modeRun self.books = [] self.config = CONFIG['books'] def book_append(self, bookname=None): if type(bookname) is type("cadena"): book_path = self.config['TXT_PATH'] + bookname + self.config[ 'TXT_EXT'] my_file = Path(book_path) if my_file.is_file(): self.books.append(bookname) write("libro agregado") with open(book_path, 'r') as content_file: content = content_file.read() for expresion in self.config['parser']: while expresion['from'] in content:
def initial_bot(use_logging=True, level_name='DEBUG'): bot = telebot.TeleBot(TOKEN) #bot = Updater(TOKEN, request_kwargs=REQUEST_KWARGS) logger = log('bot', 'bot.log', 'INFO') @bot.message_handler(commands=['start']) def start_handler(message: telebot.types.Message): bot.send_message(message.from_user.id, start_mess) logger.info(f"It's start handler. Message from {message.from_user.id}") @bot.message_handler(commands=['help']) def help_handler(message: telebot.types.Message): bot.send_message(message.from_user.id, help_mess) logger.info(f"It's start handler. Message from {message.from_user.id}") @bot.message_handler(commands=['task']) def help_handler(message: telebot.types.Message): # a = os.getcwd() task = Read_Excel(".//excel//task_c_level.xlsm", "t_task") if str(message.from_user.username) == "AndreySavinov": s = {} for KEY in task.keys(): if task[KEY][1] == "Банк" and task[KEY][2] == "Проектная" and task[KEY][3] == "Васильев": s.update({KEY: task[KEY]}) bot.send_message(message.from_user.id, task[KEY][4]) logger.info(f"Запросил задачу {message.from_user.id}") @bot.message_handler(content_types=['text']) def text_handler(message: telebot.types.Message): mmes = message.from_user if message.text == "Готово": bot.send_message(message.from_user.id, " Молодец " + str(message.from_user.first_name)+" ! "+message.from_user.username) keyboard = types.InlineKeyboardMarkup() callback_button = types.InlineKeyboardButton(text="Нажми меня", callback_data="test") callback_button2 = types.InlineKeyboardButton(text="Не нажимай меня", callback_data="notest") keyboard.add(callback_button) keyboard.add(callback_button2) bot.send_message(message.chat.id, "Я – сообщение из обычного режима", reply_markup=keyboard) logger.info(f"It's text handler. Message from {message.from_user.id}") @bot.callback_query_handler(func=lambda call: True) def callback_inline(call): # Если сообщение из чата с ботом if call.message: if call.data == "test": bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Пыщь") if call.data == "notest": bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="БАХ!!!") if use_logging: telebot.logger.setLevel(logging.getLevelName(level_name)) return bot
import time import os import cherrypy import telebot from telebot import apihelper from source.bot import initial_bot from source.config import * from source.log import log BOT = initial_bot(use_logging=True, level_name='INFO') server_logger = log('server', 'server.log', 'INFO') 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]) server_logger.info(f"New updates. Info: {[update]}") return '' else: server_logger.info(f"Server error! Error status 403") raise cherrypy.HTTPError(403)