forked from TGExplore/TG-Files-to-Link
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.py
49 lines (43 loc) · 1.72 KB
/
telegram.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
# tgfilestream - A Telegram bot that can stream Telegram files to users over HTTP.
# Copyright (C) 2019 Tulir Asokan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.'
import logging
from telethon import TelegramClient, events
from telethon.sessions import StringSession
from .paralleltransfer import ParallelTransferrer
from .config import (
session_name,
api_id,
api_hash,
public_url,
start_message,
group_chat_message
)
from .util import pack_id, get_file_name
log = logging.getLogger(__name__)
client = TelegramClient(StringSession(session_name), api_id, api_hash)
transfer = ParallelTransferrer(client)
@client.on(events.NewMessage)
async def handle_message(evt: events.NewMessage.Event) -> None:
if not evt.is_private:
await evt.reply(group_chat_message)
return
if not evt.file:
await evt.reply(start_message)
return
url = public_url / str(pack_id(evt)) / get_file_name(evt)
await evt.reply(f"Link to download file: {url}")
log.info(f"Replied with link for {evt.id} to {evt.from_id} in {evt.chat_id}")
log.debug(f"Link to {evt.id} in {evt.chat_id}: {url}")