def my_sources(message): source = Sources(db.get_connection()) sources = source.get_all() sub = Subscription(db.get_connection()) user = Users(db.get_connection()) user.get_user(tg_id=message.chat.id) subs = sub.get_users_subs(user.user_id) if not subs: bot.send_message(message.chat.id, 'у вас нет активных подписок :(\n' 'Что бы оформить подписку нажмите кнопку новости') return source_names = dict() keyboard = types.InlineKeyboardMarkup() #Клавиатура for s in sources: source_names.update({s[0]: s[1]}) print(source_names) for s in subs: id_, s_id, u_id, date = s keyboard.add(types.InlineKeyboardButton(text=source_names[s_id], callback_data=f'del:{s_id}')) question = 'Что бы отписаться нажмите на ненужные новостные порталы:' bot.send_message(message.from_user.id, text=question, reply_markup=keyboard)
def get_score(message): if not str(message.chat.id) in Configuration.admins: return user = Users(db.get_connection()) user_count = len(user.get_all()) bot.send_message(message.chat.id, f"Всего пользователей: {user_count}")
def bot_message(close_markup, cid, text): if close_markup: bot.send_message( cid, text, reply_markup=ReplyKeyboardRemove(), ) else: bot.send_message(cid, text)
def get_source(message): if not str(message.chat.id) in Configuration.admins: return callback = '' source = Sources(db.get_connection()) all_sources = source.get_all() for source_name in all_sources: callback += f"{source_name[0]}) {source_name[1]}\n" bot.send_message(message.chat.id, callback)
def start_message(message): # print(f"------------\n\tStart \n\tchat id: {message.chat.id}\n------------") bot.send_message(message.chat.id, 'Добро пожаловать в бот Агрегатор новостей! ' 'Здесь вы можете подписаться на рассылку ' 'интересующих вас новостей, которые бот ' 'будет вам отправлять. Для вывода новостных порталов ' 'напишите новости.', reply_markup=start_buttons()) user = Users(db.get_connection()) if user.get_user(tg_id=message.chat.id): return user.create(message.chat.id)
def send_message(request): response_data = {} if request.method == 'POST': data = request.data if 'clientID' in data and 'text' in data: tg_user = get_tg_user(data['clientID']) if tg_user: if tg_user.active_psy.psy_code == request.user.psy_code: message = Message( tg_user=tg_user, status=RECEIVED, text=data['text'], is_read=True ) message.save() bot.send_message( chat_id=tg_user.chat_id, text=message.text ) response_data['success'] = 'Message successfully sent to Telegram Client.' return Response( data=response_data, status=status.HTTP_201_CREATED ) else: response_data['error'] = 'It\'s not you Telegram Client.' return Response( data=response_data, status=status.HTTP_406_NOT_ACCEPTABLE, ) else: response_data['error'] = 'No Telegram Client with such ID.' return Response( data=response_data, status=status.HTTP_404_NOT_FOUND, ) else: response_data['error'] = '\'clientID\' or \'text\' didn\'t came.' return Response( data=response_data, status=status.HTTP_400_BAD_REQUEST, ) else: response_data['error'] = 'Only \'POST\' requests accepted.' return Response( data=response_data, status=status.HTTP_405_METHOD_NOT_ALLOWED, )
def remove_source(message): if not str(message.chat.id) in Configuration.admins: return source_id = message.text.split()[1] source = Sources(db.get_connection()) id_, name, url, context = source.get(source_id=source_id) source.delete() bot.send_message(message.chat.id, f"Источник {id_}:{name} был удалён!\n" f"Если вы хотите его вернуть введите:\n" f"\"/add {name} {url}\"")
def log_message(message: str, level: str = None): from .settings import settings if not message or not settings.OWNER: return _level = settings.LOG if not _level or (level != _level and _level == 'error'): return from bot.bot import bot bot.send_message(chat_id=settings.OWNER, text=message)
def start(message): keyboard = types.InlineKeyboardMarkup() #Клавиатура source = Sources(db.get_connection()) all_sources = source.get_all() sources_names = list() for source in all_sources: sources_names.append([source[1], f"add:{source[0]}"]) for res in sources_names: keyboard.add(types.InlineKeyboardButton(text=res[0], callback_data=res[1])) question = 'Выберите новостные порталы:' bot.send_message(message.from_user.id, text=question, reply_markup=keyboard)
def del_sub(tg_id, source_id): user = Users(db.get_connection()) subscription = Subscription(db.get_connection()) source = Sources(db.get_connection()) user.get_user(tg_id=tg_id) subscription.delete({ 'source_id': source_id, 'user_id': user.user_id }) source.get(source_id=source_id) print("del tg_id: ", tg_id) print("del sub_id: ", source_id) bot.send_message(tg_id, f'Подписка на "{source.name}" отменена.')
def send_message(self, user_id): keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=3) keyboard.add(*SendMessageWithPerformers.get_buttons(user_id)) message = bot.send_message(user_id, self.message_text, parse_mode="HTML", reply_markup=keyboard) return message
def rename(message): if not str(message.chat.id) in Configuration.admins: bot.send_message(message.chat.id, "это админская команда.") return sp = message.text.split(',') print(sp) try: id_, name = sp except Exception as e: print(e) return id_ = int(id_.split()[1]) print("id:", id_) print("name:", name) source = Sources(db.get_connection()) source.update({"id": id_}, {"name": name})
def add_source(message): if not str(message.chat.id) in Configuration.admins: bot.send_message(message.chat.id, "это админская команда.") return sp = message.text.split(',') print(sp) try: name, url = sp except Exception as e: print(e) return name = name[5:] print("name:", name) print("url:", url) source = Sources(db.get_connection()) source.create(name.strip(), url.strip()) bot.send_message(message.chat.id, f"new sources ({name} - {url}) added success.")
def send_telegram_message(chat: Chat, text: str, parse_mode: ParseMode = telegram.ParseMode.MARKDOWN, disable_preview: bool = True, **kwargs): if not bot: log.warning("No telegram token. Skipping") return log.info(f"Telegram: sending the message: {text}") return bot.send_message(chat_id=chat.id, text=text, parse_mode=parse_mode, disable_web_page_preview=disable_preview, **kwargs)
def send_telegram_message(chat: Chat, text: str, parse_mode: ParseMode = telegram.ParseMode.HTML, disable_preview: bool = True, **kwargs): if not bot: log.warning("No telegram token. Skipping") return log.info(f"Telegram: sending the message: {text}") try: return bot.send_message(chat_id=chat.id, text=text, parse_mode=parse_mode, disable_web_page_preview=disable_preview, **kwargs) except telegram.error.TelegramError as ex: log.warning(f"Telegram error: {ex}")
def add_sub(tg_id, source_id): user = Users(db.get_connection()) subscription = Subscription(db.get_connection()) source = Sources(db.get_connection()) user.get_user(tg_id=tg_id) source.get(source_id=source_id) print("add tg_id: ", tg_id) print("add sub_id: ", source_id) if not subscription.create(user.user_id, source_id): bot.send_message(tg_id, f'Вы уже подписаны на "{source.name}".') return bot.send_message(tg_id, f'Подписка на "{source.name}" оформлена.') bot.send_message(tg_id, source.context)