Telethon 运行一次,名称“main 未定义”

问题描述

我有这个加入频道和帖子的代码,但是每当我运行它时,它都会对 1-2 个频道执行此操作,然后我收到一个错误

回溯(最近一次调用最后一次): 文件“C:/Users/Arnoldas/PycharmProjects/pythonProject/again.py”,第 97 行,在 asyncio.run(main()) NameError: name 'main' 未定义。是什么导致了这个问题,我该如何保持代码运行?

code:

from telethon.sync import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.errors.rpcerrorlist import FloodWaitError
import time
from dotenv import load_dotenv
import os
import asyncio
channel_list=['CocktailBar_discussion','apmcoin_official','suprafinance','auroraOfficialEN','HelloLambda','AlpacaCity','themorpheusnetwork','chiliz_io','BKEXEnglish','riodefiofficial','OnXFi','DeepOnionCoin','hellochromia','AnimeBEP20','daomaker','TheCoinEx','Ochain','pNetworkDefi','devprtcl','officialxyx','powerledger','bounce_finance','uncx_token','concertVR1','obyteorg','telcoincommunity','DAAWallet','Metahash_ENG','surf_finance','dfinity','xinfintalk','xcashglobal','propsproject','BTCPrivate','OIN_Finance_Eng','mithcash','hivenetwork','tokeNown','trustverse_officialchannel','wax_io','TerraLunaChat','unit_en','FilecashGlobal','FryWorldFinance','originprotocol','LibraEcosystem','SpartanProtocolOrg','wgreenpay','cartesiproject','buymemecash','IGgalaxy','AlphaQuark','basedmoney','hybrixgroup','RYIUNITY','mftudotnet','MixDex','mobius_network'
]


client = TelegramClient('session_name',myid,myapikey
                    )
client.start()
for values in channel_list:
    try:
        destination_channel_username=values
        entity=client.get_entity(destination_channel_username)
        client(JoinChannelRequest(channel=values))
        client.send_message(entity=entity,message='Want to learn how we make crypto hedging easy? Join ProSwap Telegram channel ProSwapCommunity @ TG')
        client.send_file(values,'C:/Users/Arnoldas/Desktop/PROSWAP.png')
        time.sleep(20)
    except:
        continue
        time.sleep(20)

asyncio.run(main())

解决方法

您尚未定义 ma​​in 函数。

像这样编写代码:

from telethon.sync import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.errors.rpcerrorlist import FloodWaitError
import time
from dotenv import load_dotenv
import os
import asyncio
channel_list=['CocktailBar_Discussion','apmcoin_official','suprafinance','AuroraOfficialEN','HelloLambda','AlpacaCity','themorpheusnetwork','chiliz_io','BKEXEnglish','riodefiofficial','OnXFi','DeepOnionCoin','hellochromia','AnimeBEP20','daomaker','TheCoinEx','Ochain','pNetworkDefi','devprtcl','officialxyx','powerledger','bounce_finance','uncx_token','concertVR1','obyteorg','telcoincommunity','DAAWallet','metahash_ENG','surf_finance','dfinity','xinfintalk','xcashglobal','propsproject','BTCPrivate','OIN_Finance_Eng','mithcash','hivenetwork','tokenown','trustverse_officialchannel','wax_io','TerraLunaChat','unit_en','FilecashGlobal','FryWorldFinance','originprotocol','LibraEcosystem','SpartanProtocolOrg','wgreenpay','cartesiproject','buymemecash','IGGalaxy','AlphaQuark','basedmoney','hybrixgroup','RYIUNITY','mftudotnet','MixDex','mobius_network'
]


client = TelegramClient('session_name',myid,myapikey
                    )
client.start()
async def main():
    for values in channel_list:
        try:
            destination_channel_username=values
            entity=await client.get_entity(destination_channel_username)
            await client(JoinChannelRequest(channel=values))
            await client.send_message(entity=entity,message='Want to learn how we make crypto hedging easy? Join ProSwap Telegram channel ProSwapCommunity @ TG')
            await client.send_file(values,'C:/Users/Arnoldas/Desktop/PROSWAP.png')
            await asyncio.sleep(20)
        except:
            continue
            await asyncio.sleep(20)

asyncio.run(main())