【问题标题】:How to leave a channel or group with id in telethon?如何在 Telethon 中离开带有 id 的频道或群组?
【发布时间】:2020-09-01 19:27:06
【问题描述】:

您好,我正在尝试使用此脚本离开一个带有 id 的频道,但它没有很好地显示任何结果 LeaveChannelRequest 也不适用于我 这是我尝试过的:

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())

我的问题在哪里?

【问题讨论】:

    标签: python python-3.x telegram telethon


    【解决方案1】:

    离开频道或群组的方法调用在内部函数中定义

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

    没有在外部函数中调用,你可以避免使用内部函数。比如你可以像这样调用delete_dialog方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-09
      • 2021-08-20
      • 2020-11-01
      • 2022-01-19
      • 1970-01-01
      • 2022-01-15
      • 2022-07-16
      • 1970-01-01
      相关资源
      最近更新 更多