Skip to content

Commit

Permalink
add command /status to show pinned status message
Browse files Browse the repository at this point in the history
  • Loading branch information
hlf20010508 committed Jun 30, 2023
1 parent 7115dfe commit e19462a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 53 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ So this project use a user account to download files and a bot account to play t
That's why you need to prepare a lot of things to use this bot.

## Bot Command
- `/start` to start with bot
- `/auth` to authorize telegram and onedrive
- `/start` to start with bot.
- `/auth` to authorize telegram and onedrive.
- `/status` to show pinned status message.
- `/links message_link range` to transfer sequential restricted content.
- `/autoDelete true/false` decides whether bot can auto delete message.
- `/help` for help
- `/help` for help.

Example:
`/links https://t.me/c/xxxxxxx/100 2` will transfer `https://t.me/c/xxxxxxx/100` and `https://t.me/c/xxxxxxx/101`.

## Authorization Steps
- Send `/auth`
- Send `/auth`.
- Wait and you'll receive the login code from telegram.
- Visit the uri the bot sends, and submit the code.
- Then the bot will send the authorization uri for OneDrive, visit it and login.
Expand Down
97 changes: 48 additions & 49 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ async def delete_message(message):
if delete_flag:
await message.delete()

async def check_in_group(event):
if isinstance(event.message.peer_id, types.PeerUser):
await event.respond('''
This bot must be used in a Group or Channel!
Add this bot to a Group or Channel as Admin, and give it ability to Delete Messages.
''')
raise events.StopPropagation

async def res_not_login(event):
await event.respond('''
You haven't logined to Telegram.
Use /auth to login.
''')
raise events.StopPropagation

cmd_helper = '''/auth: Authorize for Telegram and OneDrive.
/status: Show pinned status message.
`/links` message_link range: Transfer sequential restricted content.
`/autoDelete true` to auto delete message.
`/autoDelete false` to not auto delete message.
'''

@tg_bot.on(events.NewMessage(pattern="/start", incoming=True))
async def start(event):
"""Send a message when the command /start is issued."""
Expand All @@ -79,37 +104,27 @@ async def start(event):
Forward or upload files to me, or pass message link to transfer restricted content from group or channel.
/auth: Authorize for Telegram and OneDrive.
`/links` message_link range: Transfer sequential restricted content.
`/autoDelete` true/false: Whether bot can auto delete messages.
%s
/help: Ask for help.
''')
'''%cmd_helper)
raise events.StopPropagation


@tg_bot.on(events.NewMessage(pattern="/help", incoming=True))
async def help(event):
"""Send a message when the command /help is issued."""
await event.respond('''
/auth to authorize for Telegram and OneDrive.
`/links` message_link range: Transfer sequential restricted content.
`/autoDelete` true/false: Whether bot can auto delete messages.
%s
To transfer files, forward or upload to me.
To transfer restricted content, right click the content, copy the message link, and send to me.
''')
'''%cmd_helper)
raise events.StopPropagation


@tg_bot.on(events.NewMessage(pattern="/auth", incoming=True))
async def auth(event):
if isinstance(event.message.peer_id, types.PeerUser):
await event.respond('''
This bot must be used in a Group or Channel!
Add this bot to a Group or Channel as Admin, and give it ability to Delete Messages.
''')
raise events.StopPropagation
await check_in_group(event)
auth_server = subprocess.Popen(('python', 'auth_server.py'))
async with tg_bot.conversation(event.chat_id) as conv:

Expand Down Expand Up @@ -184,6 +199,19 @@ async def auto_delete(event):
await event.respond(error_message)
raise events.StopPropagation

@tg_bot.on(events.NewMessage(pattern="/status", incoming=True))
async def status(event):
await check_in_group(event)
global status_bar
try:
async for message in tg_client.iter_messages(event.chat_id, filter=types.InputMessagesFilterPinned()):
await tg_client.unpin_message(event.chat_id, message)
except:
await res_not_login(event)
status_bar = await event.respond("Status:\n\nNo job yet.")
await tg_bot.pin_message(event.chat_id, status_bar)
raise events.StopPropagation


async def multi_parts_downloader(
client, document, path, conn_num=10, progress_callback=None
Expand Down Expand Up @@ -246,14 +274,7 @@ def get_link(string):

@tg_bot.on(events.NewMessage(pattern="/links", incoming=True))
async def links(event):
if isinstance(event.message.peer_id, types.PeerUser):
await delete_message(event)
await event.respond('''
This bot must be used in a Group or Channel!
Add this bot to a Group or Channel as Admin, and give it ability to Delete Messages.
''')
raise events.StopPropagation
await check_in_group(event)
try:
cmd = cmd_parser(event)
link = cmd[0]
Expand All @@ -266,12 +287,7 @@ async def links(event):
await tg_client.send_message(event.chat_id, message='%s/%d'%(link_body, head_message_id + offset))
except:
await delete_message(event)
await event.respond('''
You haven't logined to Telegram.
Use /auth to login.
''')
raise events.StopPropagation
await res_not_login(event)
except:
await delete_message(event)
await event.respond('''
Expand Down Expand Up @@ -304,26 +320,14 @@ async def upload(local_path):
await event.reply('Done.')
await tg_bot.edit_message(status_bar, 'Status:\n\nNo job yet.')

if isinstance(event.message.peer_id, types.PeerUser):
await delete_message(event)
await event.respond('''
This bot must be used in a Group or Channel!
Add this bot to a Group or Channel as Admin, and give it ability to Delete Messages.
''')
raise events.StopPropagation
await check_in_group(event)

if event.media and not isinstance(event.media, types.MessageMediaWebPage):
try:
message = await tg_client.get_messages(event.message.peer_id, ids=event.message.id)
except:
await delete_message(event)
await event.respond('''
You haven't logined to Telegram.
Use /auth to login.
''')
raise events.StopPropagation
await res_not_login(event)

try:
if "document" in event.media.to_dict().keys():
Expand Down Expand Up @@ -371,12 +375,7 @@ async def upload(local_path):
message = await tg_client.get_messages(chat, ids=msg_id)
except:
await delete_message(event)
await event.respond('''
You haven't logined to Telegram.
Use /auth to login.
''')
raise events.StopPropagation
await res_not_login(event)
if message:
try:
if "document" in message.media.to_dict().keys():
Expand Down

0 comments on commit e19462a

Please sign in to comment.