-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
114 lines (90 loc) · 4.53 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# importing the necessary modules
import os
import random
import telebot
from telebot.types import InlineKeyboardButton
from telebot.types import InlineKeyboardMarkup
from telebot.types import InputTextMessageContent
from telebot.types import InlineQueryResultCachedSticker
API_KEY = os.getenv("API-KEY") # API TOKEN from @BotFather
# storing the file_ids from Telegram servers
BANNER = "AgACAgUAAxkBAAMDYoeu5WbkziZdcibcP9llPILJS_\
kAAjK0MRtvCkFUmAmFH6vvxjkBAAMCAAN5AAMkBA"
COIN = "CAACAgIAAxkBAAMJYoexj1eJxVQY0R98jqcX0ZaQqhwA\
Ar4JAAKErphKEkME6E2wM7IkBA"
HEADS = "CAACAgUAAxkBAAMHYoewFSFllpNDI2suypkk2H3DaRsA\
AkYFAALwJzFU0tHe9ydf8jgkBA"
TAILS = "CAACAgUAAxkBAAMIYoewMk78FlKO_5xAAr38_r9KCQwA\
Ai8GAAIBWDBUh_A_KXLBzrUkBA"
art = [ # stylish fonts for heads and tails
"""<code>\
█░░█ █▀▀ █▀▀█ █▀▀▄ █▀▀
█▀▀█ █▀▀ █▄▄█ █░░█ ▀▀█
▀░░▀ ▀▀▀ ▀░░▀ ▀▀▀░ ▀▀▀
</code>"""
,
"""<code>\
▀▀█▀▀ █▀▀█ ░▀░ █░░ █▀▀
░░█░░ █▄▄█ ▀█▀ █░░ ▀▀█
░░▀░░ ▀░░▀ ▀▀▀ ▀▀▀ ▀▀▀
</code>"""
]
bot = telebot.TeleBot(API_KEY, parse_mode="HTML") # Initializing the bot
def flip(): # Defining a toss function using random module
coin = {0: ["HEADS", HEADS], 1: ["TAILS", TAILS]}
return coin[random.choice([0, 1])]
@bot.message_handler(commands="start") # /start command
def start(message):
bot.send_photo(message.chat.id, BANNER, caption="<b>Can't seem to decide on something?\n\
\nLet me help you! 😄\n\nChoose Heads or Tails and press /toss.\n\
\nYou can also use me in other chats too!\n\nJust tag @tossflipbot and toss! 😉</b>",
reply_markup = InlineKeyboardMarkup().row(InlineKeyboardButton(
"Try me in other chats! 💬", switch_inline_query="")))
@bot.message_handler(commands="toss") # /toss command
def toss(message):
bot.send_sticker(message.chat.id, COIN, reply_markup = InlineKeyboardMarkup().row(
InlineKeyboardButton("Flip a coin! 🪙", callback_data="toss")))
@bot.inline_handler(lambda query: True) # Inline Mode for any chat
def toss_inline(query):
try:
value = random.randint(0, 1)
if not value:
score = "1-0"
counter_button = InlineKeyboardButton(
"|| HEADS - 1 || TAILS - 0 ||", callback_data="score")
else:
score = "0-1"
counter_button = InlineKeyboardButton(
"|| HEADS - 0 || TAILS - 1 ||", callback_data="score")
toss = InlineQueryResultCachedSticker(f'toss-{random.randint(0, 1)}', COIN,
reply_markup= InlineKeyboardMarkup(row_width=1).add(*[counter_button, InlineKeyboardButton(
"Flip again! 🪙", callback_data=f"toss-inline::{score}")]),
input_message_content=InputTextMessageContent(art[value], parse_mode="HTML"))
bot.answer_inline_query(query.id, [toss])
except: pass
@bot.callback_query_handler(func=lambda call: call.data[:11] == 'toss-inline')
def callback_listener(call): # Handles all the inline callbacks
inline_id, score = call.data.split("::")
heads, tails = map(int, score.split("-"))
value = random.randint(0, 1)
if not value:
score = f"{heads+1}-{tails}"
counter_button = InlineKeyboardButton(
f"|| HEADS - {heads+1} || TAILS - {tails} ||", callback_data="score")
else:
score = f"{heads}-{tails+1}"
counter_button = InlineKeyboardButton(
f"|| HEADS - {heads} || TAILS - {tails+1} ||", callback_data="score")
bot.edit_message_text(art[value], inline_message_id=call.inline_message_id,
reply_markup=InlineKeyboardMarkup(row_width=1).add(*[
counter_button, InlineKeyboardButton("Flip again! 🪙",
callback_data=f"toss-inline::{score}")]))
@bot.callback_query_handler(func=lambda call: call.data == 'toss')
def callback_listener(call): # Handles all the private chat callbacks
result = flip()
bot.delete_message(call.message.chat.id, call.message.id)
bot.send_sticker(call.message.chat.id, result[-1], reply_markup=InlineKeyboardMarkup(
row_width=1).add(*[InlineKeyboardButton(result[0], callback_data=result[0]),
InlineKeyboardButton("Flip again! 🪙", callback_data="toss")]))
bot.infinity_polling( # Starts the polling
skip_pending=True)