Skip to content

Instantly share code, notes, and snippets.

@AndreiChenchik
Last active March 3, 2023 12:59
Show Gist options
  • Save AndreiChenchik/e83dd14de6d6cbc60d4139c8b35e2388 to your computer and use it in GitHub Desktop.
Save AndreiChenchik/e83dd14de6d6cbc60d4139c8b35e2388 to your computer and use it in GitHub Desktop.
Telegram bot examples
import telebot
bot = telebot.TeleBot("6000754704:AAExaMDxy3hbKmNBPjVaHOy4po-7q6VPZ0I", parse_mode=None)
@bot.message_handler(commands=['new'])
def send_welcome(message):
blocks = message.text.split(" ")
number = blocks[1]
markup = telebot.types.InlineKeyboardMarkup()
markup.row_width = 2
markup.add(
telebot.types.InlineKeyboardButton("Yes", callback_data="cb_yes"),
telebot.types.InlineKeyboardButton("No", callback_data="cb_no")
)
bot.reply_to(message, number, reply_markup=markup)
@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
markup = telebot.types.InlineKeyboardMarkup()
markup.row_width = 2
markup.add(
telebot.types.InlineKeyboardButton("Yes", callback_data="cb_yes"),
telebot.types.InlineKeyboardButton("No", callback_data="cb_no")
)
if call.data == "cb_yes":
bot.edit_message_text(text="yes", chat_id=call.message.chat.id, message_id=call.message.id, reply_markup=markup)
elif call.data == "cb_no":
bot.edit_message_text(text="no", chat_id=call.message.chat.id, message_id=call.message.id, reply_markup=markup)
bot.infinity_polling()
import telebot
from dataclasses import dataclass, field
from functools import reduce
import operator
import uuid
bot = telebot.TeleBot("TOKEN")
@dataclass
class Participant:
id: int
name: str
_slots: int = 0
@property
def description(self) -> str:
message = f"[{self.name}](tg://user?id={self.id})"
if self.used_slots > 1:
message += f" \\+{self.used_slots-1}"
return message
@property
def used_slots(self) -> int:
return self._slots
@used_slots.setter
def used_slots(self, value: int):
self._slots = max(value, 0)
@dataclass
class Game:
title: str
limit: int
id: uuid.UUID = field(default_factory=uuid.uuid4)
participants: dict[int, Participant] = field(default_factory=dict)
@property
def description(self) -> str:
number = self.participants_count
message = f"*{self.title}*"
if number != self.limit:
message += f"\nСвободно {self.limit-number} из {self.limit} мест"
else:
message += "\nВсе места заняты"
participants = list(
filter(lambda x: x.used_slots > 0, self.participants.values()))
if len(participants) > 0:
message += "\n\nЗаписались:"
message += "\n \\- "
message += "\n \\- ".join(map(lambda x: x.description, participants))
return message
@property
def participants_count(self) -> int:
used_slots = map(lambda x: x.used_slots, self.participants.values())
return reduce(operator.add, used_slots, 0)
games: dict[str, Game] = {}
def game_markup(id: str) -> telebot.types.InlineKeyboardMarkup:
markup = telebot.types.InlineKeyboardMarkup()
markup.row_width = 2
markup.add(
telebot.types.InlineKeyboardButton("➕", callback_data=f"add {id}"),
telebot.types.InlineKeyboardButton("➖", callback_data=f"rem {id}"))
return markup
bot.delete_my_commands(scope=None, language_code=None)
bot.set_my_commands(
commands=[
telebot.types.BotCommand("new", "/new 10 Saturday at 1:00 AM")
]
)
@bot.message_handler(commands=['new'])
def new_game(message: telebot.types.Message):
if message.text is None: return
limit = int(message.text.split()[1])
title = " ".join(message.text.split()[2:])
game = Game(title=title, limit=limit)
game_id = game.id.hex
games[game_id] = game
bot.send_message(
chat_id=message.chat.id,
text=game.description,
parse_mode='MarkdownV2',
reply_markup=game_markup(id=game_id)
)
@bot.callback_query_handler(func=lambda call: True)
def button_callback(call: telebot.types.CallbackQuery):
action = call.data.split()[0]
game_id = call.data.split()[1]
user = call.from_user
game = games.get(game_id)
if game is None:
bot.edit_message_text(
text="*ERROR, PLEASE RECREATE EVENT*",
chat_id=call.message.chat.id,
message_id=call.message.id,
parse_mode='MarkdownV2'
)
return
participant = game.participants.get(
user.id,
Participant(id=user.id, name=user.full_name))
if action == "add":
participant.used_slots += 1
elif action == "rem":
participant.used_slots -= 1
game.participants[user.id] = participant
if game.participants_count <= game.limit:
bot.edit_message_text(
text=game.description,
chat_id=call.message.chat.id,
message_id=call.message.id,
parse_mode='MarkdownV2',
reply_markup=game_markup(id=game_id)
)
bot.infinity_polling()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment