-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
81 lines (60 loc) · 3.01 KB
/
script.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
import os
from dotenv import load_dotenv
from telegram import Update, ParseMode
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
from telegraph import Telegraph
from urllib.parse import urlparse
# Load environment variables from .env file
load_dotenv()
# Telegram Bot Token from environment variable
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
# Telegraph Token from environment variable
TELEGRAPH_TOKEN = os.getenv('TELEGRAPH_TOKEN')
# Initialize Telegraph
telegraph = Telegraph(TELEGRAPH_TOKEN)
# Initialize Updater
updater = Updater(token=TELEGRAM_TOKEN, use_context=True)
# Command handler for /start command
def start(update: Update, context: CallbackContext) -> None:
user = update.effective_user
context.bot.send_message(chat_id=update.effective_chat.id, text=f"Welcome, {user.first_name}!")
# Command handler for /help command
def help(update: Update, context: CallbackContext) -> None:
help_message = "This is a file filter bot. It can perform various operations on files.\n\n" \
"Available commands:\n" \
"/start - Start the bot\n" \
"/help - Show help message\n" \
"/shorten - Shorten a URL\n" \
"/filter - Filter files\n"
context.bot.send_message(chat_id=update.effective_chat.id, text=help_message)
# Command handler for /shorten command
def shorten(update: Update, context: CallbackContext) -> None:
url = context.args[0] if len(context.args) > 0 else None
if url:
# Shorten the URL using Telegraph
shortened_url = telegraph.create_page(url=url)['url']
context.bot.send_message(chat_id=update.effective_chat.id, text=f"Shortened URL: {shortened_url}")
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="Please provide a URL to shorten.")
# Command handler for /filter command
def filter_files(update: Update, context: CallbackContext) -> None:
file_name = context.args[0] if len(context.args) > 0 else None
if file_name:
# Filter files based on the provided file name
# Implement your logic here
context.bot.send_message(chat_id=update.effective_chat.id, text=f"Filtering files with name: {file_name}")
else:
context.bot.send_message(chat_id=update.effective_chat.id, text="Please provide a file name to filter.")
# Message handler for handling all other messages
def handle_message(update: Update, context: CallbackContext) -> None:
# Handle other message types
context.bot.send_message(chat_id=update.effective_chat.id, text="Unknown command or message.")
# Set up command handlers
updater.dispatcher.add_handler(CommandHandler("start", start))
updater.dispatcher.add_handler(CommandHandler("help", help))
updater.dispatcher.add_handler(CommandHandler("shorten", shorten))
updater.dispatcher.add_handler(CommandHandler("filter", filter_files))
updater.dispatcher.add_handler(MessageHandler(Filters.all, handle_message))
# Start the bot
updater.start_polling()
updater.idle()