gpt4 book ai didi

python - 属性错误 : 'coroutine' object has no attribute 'username'

转载 作者:行者123 更新时间:2023-12-05 04:44:31 33 4
gpt4 key购买 nike

嗨!我是新手,我请求帮助。

所以我从 github 下载了这段名为 Telegram 自动回复的代码并运行了它,但随后出现了这个错误:

"C:\Users\user\Downloads\Compressed\telegram-auto-reply-master\main.py", line 36, in handle_new_message
me = await client.get_me().username
AttributeError: 'coroutine' object has no attribute 'username'

here is the screenshot

这是 main.py 代码:

#!/usr/bin/env python3
# A simple script to print some messages.
import time
import re
import json
import random
import os
from asyncio import sleep
from pprint import pprint

from telethon import TelegramClient, events, utils
from dotenv import load_dotenv

load_dotenv() # get .env variable

session = os.environ.get('TG_SESSION', 'printer')
api_id = os.getenv("API_ID")
api_hash = os.getenv("API_HASH")
debug_mode = os.getenv("DEBUG_MODE").upper() == "TRUE"

proxy = None # https://github.com/Anorov/PySocks

# Create and start the client so we can make requests (we don't here)
client = TelegramClient(session, api_id, api_hash, proxy=proxy).start()

# create a sender list to check if user already send private message or mention
senderList = []

#read json file and prepare quiz to send later
with open('quizzes.json') as json_file:
quizzes = json.load(json_file)

@client.on(events.NewMessage)
async def handle_new_message(event):

me = await client.get_me().username
from_ = await event.client.get_entity(event.from_id) # this lookup will be cached by telethon
to_ = await event.client.get_entity(event.message.to_id)

needToProceed = from_.is_self if debug_mode else not from_.is_self and (event.is_private or re.search("@"+me.username,event.raw_text))
if needToProceed: # only auto-reply to private chats: # only auto-reply to private chats
if not from_.bot and event: # don't auto-reply to bots
print(time.asctime(), '-', event.message) # optionally log time and message
await sleep(1) # pause for 1 second to rate-limit automatic replies
message = ""
senderList.append(to_.id)
if senderList.count(to_.id) < 2:
message = f"""**AUTO REPLY**
\nHi @{from_.username},
\n\nMohon maaf boss saya sedang offline, mohon tunggu sebentar.
\nSilahkan lihat-lihat [imacakes](https://www.instagram.com/ima_cake_cirebon) dulu untuk cuci mata.
\n\n**AUTO REPLY**"""
elif senderList.count(to_.id) < 3:
message = f"""**AUTO REPLY**
\nMohon bersabar @{from_.username}, boss saya masih offline 😒"""
elif senderList.count(to_.id) < 4:
message = f"""**AUTO REPLY**
\n@{from_.username} Tolong bersabar yaa 😅"""
else:
random_number = random.randint(0,len(quizzes) - 1)
question = quizzes[random_number]['question']
answer = quizzes[random_number]['answer']
message = f"""**AUTO REPLY**
\n @{from_.username}, Main tebak-tebakan aja yuk 😁
\n {question}
\n {answer}
\n """

if message != "":
await event.reply(message)

client.start()
client.run_until_disconnected()

我真的不知道出了什么问题,因为我是新手,对编码知之甚少,所以如果有人愿意帮助我,我真的很感激 :)

感谢任何人帮助我 :)

最佳答案

我认为问题出在 me = await client.get_me().username 上。

根据Telethon Quick-Start , client.get_me() 是一个 awaitable .这意味着要获得返回值,您必须 await 它,如 await client.get_met()。在此之前,client.get_me() 的结果是 coroutine .因此 await client.get_me().username 不起作用,因为当您尝试访问 username 属性时 client.get_me() 仍然是协程。

快速入门实际上是这样做的

me = await client.get_me()
username = me.username

关于python - 属性错误 : 'coroutine' object has no attribute 'username' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69355743/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com