caption=random_file
glob.glob(f"test/*.{ext}")
import time
import glob, os, random
from variables import *
import telebot
from telebot import types
bot = telebot.TeleBot(TOKEN)
content_types = ["text"]
@bot.message_handler(commands=['start'])
def start_handler(message):
keyboard = types.InlineKeyboardMarkup(row_width=3)
url_button = types.InlineKeyboardButton(text="TEXT", url="https://t.me")
url_buttons = types.InlineKeyboardButton(text="TEXT", url="https://t.me")
url_buttons1 = types.InlineKeyboardButton(text="TEXT", url="https://t.me")
keyboard.add(url_button)
keyboard.add(url_buttons)
keyboard.add(url_buttons1)
bot.send_message(message.chat.id, f'Привет, {message.from_user.first_name}!')
bot.send_message(message.chat.id, "ТЕКСТ!", reply_markup=keyboard)
@bot.message_handler(commands=['photo'])
def photo_handler(message):
keyboard = types.InlineKeyboardMarkup(row_width=1)
data_buttons1 = types.InlineKeyboardButton(text="Получить!", callback_data="requestphoto1")
keyboard.add(data_buttons1)
bot.send_message(message.chat.id, "Кнопка для получения фото:", reply_markup=keyboard)
@bot.callback_query_handler(func=lambda call: call.data=="requestphoto1")
def test_callback(call):
files = []
for ext in ["png", "jpg", "jpeg"]:
[files.append(file) for file in glob.glob(f"*.{ext}")]
random_file = files[random.randint(0, len(files)-1)]
with open(random_file, 'rb') as f:
bot.send_media_group(call.message.chat.id, [types.InputMediaPhoto(f, caption=random_file)])
bot.answer_callback_query(callback_query_id=call.id, text="Отправлено!")
@bot.message_handler(content_types=['text'])
def txt(message):
for i in range(0, len(bad_words)):
if bad_words[i] in message.text.lower():
try:
bot.delete_message(message.chat.id, message.message_id, )
print(message.text + " delited")
except OSError:
print("BadWordsError - Sending again after 3 seconds!!!")
time.sleep(3)
bot.delete_message(message.chat.id, message.message_id)
print(message.text + " delited")
if __name__ == '__main__':
try:
bot.polling(none_stop=True)
except OSError:
print("PollingError - Sending again after 5 seconds!!!")
time.sleep(5)
bot.polling(none_stop=True)
import logging
import asyncio
from aiogram import Bot, Dispatcher, executor, types
from aiogram.types import CallbackQuery, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton
from contextlib import suppress
from aiogram import types
from aiogram.utils.exceptions import (MessageToEditNotFound, MessageCantBeEdited, MessageCantBeDeleted,
MessageToDeleteNotFound)
# Configure logging
logging.basicConfig(level=logging.INFO)
# Initialize bot and dispatcher
API_TOKEN = "0000000000XXXXXXXXXX"
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
# keyboards
inline_btn_1 = InlineKeyboardButton('Delete this message', callback_data='del1')
inline_kb1 = InlineKeyboardMarkup().add(inline_btn_1)
async def delete_message(message: types.Message, sleep_time: int = 0):
await asyncio.sleep(sleep_time)
with suppress(MessageCantBeDeleted, MessageToDeleteNotFound):
await message.delete()
@dp.message_handler(commands=['start', 'help'])
async def send_welcome(message: types.Message):
"""
This handler will be called when user sends `/start` or `/help` command
"""
await message.reply("Hi!\nI'm EchoBot with delete messages!\nPowered by aiogram.",
reply_markup=inline_kb1)
@dp.message_handler()
async def echo(message: types.Message):
msg = await message.answer(message.text, reply_markup=inline_kb1)
# @dp.callback_query_handler(func=lambda c: c.data == 'del1')
@dp.callback_query_handler()
async def process_callback_del1(callback_query: types.CallbackQuery):
await bot.answer_callback_query(callback_query.id, text="Message deleted!")
msg = callback_query.message
rmsg = callback_query.message.reply_to_message
asyncio.create_task(delete_message(msg, 1))
asyncio.create_task(delete_message(rmsg, 5))
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)