Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
TELEGRAM BUBBLE BOT TUTORIAL | |
GitHub: TraxDinosaur | |
Replit: TraxDinosaur | |
Instagram: @TraxDinosaur | |
YouTube: @TraxDinosaur | |
Blogger: TraxDinosaurs | |
''' | |
import logging | |
import requests | |
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup | |
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, CallbackQueryHandler | |
logging.basicConfig( | |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
level=logging.INFO | |
) | |
def make_bubble(text, animated, orientation, xxx): | |
url = "https://pixelspeechbubble.com/make-bubble" | |
data = { | |
"text": text, | |
"animated": animated, | |
"orientation": orientation, | |
"xxx": xxx | |
} | |
r = requests.post(url, data) | |
response = r.json() | |
image_url = response['image'] | |
if image_url.startswith("//"): | |
image_url = image_url[2:] | |
return image_url | |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
await context.bot.send_message( | |
chat_id=update.effective_chat.id, | |
text="Welcome! Use /bubble {text} to create a speech bubble." | |
) | |
async def bubble(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
user_input = ' '.join(context.args) | |
if not user_input: | |
await context.bot.send_message( | |
chat_id=update.effective_chat.id, | |
text="Please provide text for the bubble." | |
) | |
return | |
keyboard = [ | |
[InlineKeyboardButton("Animated: True", callback_data=f'animated_true_{user_input}')], | |
[InlineKeyboardButton("Animated: False", callback_data=f'animated_false_{user_input}')], | |
[InlineKeyboardButton("Orientation: Right", callback_data=f'orientation_right_{user_input}')], | |
[InlineKeyboardButton("Orientation: Left", callback_data=f'orientation_left_{user_input}')], | |
[InlineKeyboardButton("Speech", callback_data=f'xxx_speech_{user_input}')], | |
[InlineKeyboardButton("Thought", callback_data=f'xxx_thought_{user_input}')] | |
] | |
reply_markup = InlineKeyboardMarkup(keyboard) | |
message = await context.bot.send_message( | |
chat_id=update.effective_chat.id, | |
text="Please select the options for your speech bubble.", | |
reply_markup=reply_markup | |
) | |
context.user_data['bubble_message_id'] = message.message_id | |
context.user_data['bubble_options'] = { | |
'text': user_input, | |
'animated': None, | |
'orientation': None, | |
'xxx': None | |
} | |
async def bbl(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
if update.message.reply_to_message: | |
replied_message = update.message.reply_to_message | |
user_input = replied_message.text | |
keyboard = [ | |
[InlineKeyboardButton("Animated: True", callback_data=f'animated_true_{user_input}')], | |
[InlineKeyboardButton("Animated: False", callback_data=f'animated_false_{user_input}')], | |
[InlineKeyboardButton("Orientation: Right", callback_data=f'orientation_right_{user_input}')], | |
[InlineKeyboardButton("Orientation: Left", callback_data=f'orientation_left_{user_input}')], | |
[InlineKeyboardButton("Speech", callback_data=f'xxx_speech_{user_input}')], | |
[InlineKeyboardButton("Thought", callback_data=f'xxx_thought_{user_input}')] | |
] | |
reply_markup = InlineKeyboardMarkup(keyboard) | |
message = await context.bot.send_message( | |
chat_id=update.effective_chat.id, | |
text="Please select the options for your speech bubble.", | |
reply_markup=reply_markup | |
) | |
context.user_data['bubble_message_id'] = message.message_id | |
context.user_data['bubble_options'] = { | |
'text': user_input, | |
'animated': None, | |
'orientation': None, | |
'xxx': None | |
} | |
else: | |
await context.bot.send_message( | |
chat_id=update.effective_chat.id, | |
text="Please reply to a message with /bbl to use its text." | |
) | |
async def button(update: Update, context: ContextTypes.DEFAULT_TYPE): | |
query = update.callback_query | |
data = query.data.split('_') | |
if len(data) != 3: | |
await query.answer(text="Invalid selection.") | |
return | |
option_type, option_value, text = data | |
# Store selected options | |
if option_type == 'animated': | |
context.user_data['bubble_options']['animated'] = option_value | |
elif option_type == 'orientation': | |
context.user_data['bubble_options']['orientation'] = option_value | |
elif option_type == 'xxx': | |
context.user_data['bubble_options']['xxx'] = option_value | |
# Check if all options are selected | |
if all(value is not None for value in context.user_data['bubble_options'].values()): | |
# All options are selected, generate and send the bubble | |
options = context.user_data['bubble_options'] | |
image_url = make_bubble( | |
options['text'], | |
options['animated'], | |
options['orientation'], | |
options['xxx'] | |
) | |
# Determine if it's a GIF or Image | |
if image_url.lower().endswith('.gif'): | |
await query.message.reply_animation(animation=image_url) | |
elif image_url.lower().endswith(('.png', '.jpg', '.jpeg')): | |
await query.message.reply_photo(photo=image_url) | |
else: | |
await query.message.reply_text(text="The file type is not supported.") | |
# Remove the original message with options | |
bubble_message_id = context.user_data.get('bubble_message_id') | |
if bubble_message_id: | |
await context.bot.delete_message( | |
chat_id=query.message.chat_id, | |
message_id=bubble_message_id | |
) | |
# Clear user data | |
context.user_data.clear() | |
else: | |
# Check if there are changes to be made before editing | |
current_text = query.message.text | |
current_markup = query.message.reply_markup | |
if (current_text != "Please select the options for your speech bubble." or | |
current_markup != query.message.reply_markup): | |
await query.message.edit_text( | |
text="Please select the options for your speech bubble.", | |
reply_markup=query.message.reply_markup | |
) | |
await query.answer() | |
print("Bot Is Working") | |
app = ApplicationBuilder().token("YOUR_BOT_TOKEN").build() | |
app.add_handler(CommandHandler('start', start)) | |
app.add_handler(CommandHandler('bubble', bubble)) | |
app.add_handler(CommandHandler('bbl', bbl)) | |
app.add_handler(CallbackQueryHandler(button)) | |
app.run_polling() |
0 Comments