Jump to content

Python hourly auto message in telegram with cron on Linux.


or1k

Recommended Posts

Use this script however you want.
Source code of main.py

import config, re
from aiogram import Bot, Dispatcher, executor
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton

num_regex = r"\d+"
regex = r"\[\d+\]"

bot = Bot(config.BOT_TOKEN)
dp = Dispatcher(bot)

watch_board = InlineKeyboardMarkup()
watch_board.add(InlineKeyboardButton(text=config.BUTTON_TEXT, callback_data="to_watch"))

async def add_user(calldata):
	text_list = calldata.message.text.split("\n")
	calldata.message.text = ""
	for i in text_list:
		is_user = bool(len(re.findall(regex, i)))
		if is_user:
			calldata.message.text += f'<a href="tg://user?id={re.findall(num_regex, re.findall(regex, i)[-1])[0]}">{i}</a>' + "\n"
		else:
			calldata.message.text += i + "\n"
	calldata.message.text += f'<a href="tg://user?id={calldata.from_user.id}">@{calldata.from_user.first_name}[{calldata.from_user.id}]</a>' + "\n"
	return calldata

async def remove_user(calldata):
	text_list = calldata.message.text.split("\n")
	calldata.message.text = ""
	if len(text_list) > 4:
		for i in text_list:
			if str(calldata.from_user.id) not in i:
				is_user = bool(len(re.findall(regex, i)))
				if is_user:
					calldata.message.text += f'<a href="tg://user?id={re.findall(num_regex, re.findall(regex, i)[-1])[0]}">{i}</a>' + "\n"
				else:
					calldata.message.text += i + "\n"
	else:
		calldata.message.text = text_list[0]
	return calldata

@dp.callback_query_handler()
async def query_handler(calldata):
	if calldata.data == "to_watch":
		if config.TEXT_APPEND not in calldata.message.text:
			calldata.message.text = f"""{config.TEXT}

{config.TEXT_APPEND}"""
		if str(calldata.from_user.id) not in calldata.message.text:
			calldata = await add_user(calldata)
			await calldata.message.edit_text(calldata.message.text, parse_mode="html", reply_markup=watch_board)
		else:
			calldata = await remove_user(calldata)
			await calldata.message.edit_text(calldata.message.text, parse_mode="html", reply_markup=watch_board)

executor.start_polling(dp)

hour_message.py for cron

import telebot, config, asyncio
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton

watch_board = InlineKeyboardMarkup()
watch_board.add(InlineKeyboardButton(text=config.BUTTON_TEXT, callback_data="to_watch"))

bot = telebot.TeleBot(config.BOT_TOKEN)

bot.send_message(config.CHAT_ID, config.TEXT, reply_markup=watch_board)

And config.py

BOT_TOKEN = ""
CHAT_ID = -1001881205544

TEXT = "Your custom text for message default edit"

TEXT_APPEND = "First click on button (first edit) text"

BUTTON_TEXT = "Inline button text"

## DO NOT TOUCH
for i in [[BOT_TOKEN, "Bot Token"], [CHAT_ID, "Chat ID"], [TEXT, "Text"], [TEXT_APPEND, "Text Append"], [BUTTON_TEXT, "Button Text"]]:
	if i[0] == None:
		raise Exception(f"{i[1]} is not defined.")

 

proj.rar

  • Like 1
Link to comment
Share on other sites

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
  • Create New...

Important Information

By using this site you automatically agree to the Privacy Policy | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.