首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >telethon运行一次,名称'main is not defined‘

telethon运行一次,名称'main is not defined‘
EN

Stack Overflow用户
提问于 2021-04-01 16:43:13
回答 1查看 57关注 0票数 0

我有这个连接频道和帖子的代码,但是每当我运行这个代码时,它对1-2个频道执行这个操作,然后我得到一个错误:

回溯(最近一次调用):未定义asyncio.run( "C:/Users/Arnoldas/PycharmProjects/pythonProject/again.py",())中的文件主行97 NameError:名称'main‘。是什么导致了这个问题?我怎样才能让代码保持运行?

代码语言:javascript
复制
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',
'bounce_finance',
'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',
'mithcash',
'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())
EN

回答 1

Stack Overflow用户

发布于 2021-04-01 16:58:11

您尚未定义main函数。

像这样写你的代码:

代码语言:javascript
复制
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',
'bounce_finance',
'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',
'mithcash',
'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())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66901220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档