forked from teletips/MediaToTelegraphLink-TeLeTiPs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediatotelegraphlink.py
76 lines (65 loc) · 3.12 KB
/
mediatotelegraphlink.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
#Copyright ©️ 2022 TeLe TiPs. All Rights Reserved
#You are free to use this code in any of your project, but you MUST include the following in your README.md (Copy & paste)
# ##Credits - [MediaToTelegraphLink bot by TeLe TiPs] (https://github.com/teletips/MediaToTelegraphLink-TeLeTiPs)
from pyrogram import Client, filters
from pyrogram.types import Message
from telegraph import upload_file
import os
teletips=Client(
"MediaToTelegraphLink",
api_id = int(os.environ["API_ID"]),
api_hash = os.environ["API_HASH"],
bot_token = os.environ["BOT_TOKEN"]
)
@teletips.on_message(filters.command('start') & filters.private)
async def start(client, message):
text = f"""
Heya {message.from_user.mention},
I am here to generate Telegraph links for your media files.
Simply send a valid media file directly to this chat.
Valid file types are 'jpeg', 'jpg', 'png', 'mp4' and 'gif'.
To generate links in **group chats**, add me to your supergroup and send the command <code>/tl</code> as a reply to a valid media file.
🏠 | [Home](https://t.me/teletipsofficialchannel)
"""
await teletips.send_message(message.chat.id, text, disable_web_page_preview=True)
@teletips.on_message(filters.media & filters.private)
async def get_link_private(client, message):
try:
text = await message.reply("Processing...")
async def progress(current, total):
await text.edit_text(f"📥 Downloading media... {current * 100 / total:.1f}%")
try:
location = f"./media/private/"
local_path = await message.download(location, progress=progress)
await text.edit_text("📤 Uploading to Telegraph...")
upload_path = upload_file(local_path)
await text.edit_text(f"**🌐 | Telegraph Link**:\n\n<code>https://telegra.ph{upload_path[0]}</code>")
os.remove(local_path)
except Exception as e:
await text.edit_text(f"**❌ | File upload failed**\n\n<i>**Reason**: {e}</i>")
os.remove(local_path)
return
except Exception:
pass
@teletips.on_message(filters.command('tl'))
async def get_link_group(client, message):
try:
text = await message.reply("Processing...")
async def progress(current, total):
await text.edit_text(f"📥 Downloading media... {current * 100 / total:.1f}%")
try:
location = f"./media/group/"
local_path = await message.reply_to_message.download(location, progress=progress)
await text.edit_text("📤 Uploading to Telegraph...")
upload_path = upload_file(local_path)
await text.edit_text(f"**🌐 | Telegraph Link**:\n\n<code>https://telegra.ph{upload_path[0]}</code>")
os.remove(local_path)
except Exception as e:
await text.edit_text(f"**❌ | File upload failed**\n\n<i>**Reason**: {e}</i>")
os.remove(local_path)
return
except Exception:
pass
print("Bot is alive!")
teletips.run()
#Copyright ©️ 2022 TeLe TiPs. All Rights Reserved