简体   繁体   中英

How to leave a channel or group with id in telethon?

Hi I'm trying to leave a channel with id with this script but it not showing any results well LeaveChannelRequest doesn't worked for me too here is what i tried:

import asyncio
import logging
import re
import time
import os
import sys
import requests

logging.basicConfig(level=logging.ERROR)

from telethon import TelegramClient, events
from telethon.tl.functions.channels import LeaveChannelRequest
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.tl.functions.messages import GetBotCallbackAnswerRequest
from datetime import datetime
from colorama import Fore, init as color_ama
color_ama(autoreset=True)

os.system('cls' if os.name=='nt' else 'clear')

# my.telegram.org values, get your own there
api_id = ''
api_hash = ''

input_channel = '-1001226909513'
def print_msg_time(message):
    print('[' + Fore.YELLOW + f'{datetime.now().strftime("%H:%M:%S")}' + Fore.RESET + f'] {message}')
async def main():
    if len(sys.argv) < 2:
        print('Usage: python start.py phone_number')
        print('-> Input number in international format (example: +10123456789)\n')
        e = input('Press any key to exit...')
        exit(1)

    phone_number = sys.argv[1]

    if not os.path.exists("session"):
        os.mkdir("session")
    client = TelegramClient('session/' + phone_number, api_id, api_hash)
    await client.start(phone_number)
    me = await client.get_me()
    print(Fore.GREEN + f'               Current account: {me.first_name}({me.username})\n' + Fore.RESET)
    print_msg_time('leaving channels')
    async def main():
        await client.delete_dialog(input_channel)
        print_msg_time(f'{input_channel} has been leaved')

asyncio.get_event_loop().run_until_complete(main())

where is my problem?

The method call to leave the channel or group is defined in inner function

[...]
print_msg_time('leaving channels')
async def main():
    await client.delete_dialog(input_channel)
    print_msg_time(f'{input_channel} has been leaved')

which isn't called in outer function, you could avoid using a inner function. for example, you can call delete_dialog method like this

[...]
print_msg_time('leaving channels')
await client.delete_dialog(input_channel)
print_msg_time(f'{input_channel} has been leaved')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM