python - 无法从电报(电话)中获取消息
问题描述
我想用 Telethon python 自动回复消息。但是当我运行时,它无法收到消息并且无法回复我的消息。
这是我的代码。任何人都可以帮助我
def chatbot_response(msg):
ints = predict_class(msg, model)
res = getResponse(ints, intents)
return res
if __name__ == '__main__':
# Create the client and connect
# use sequential_updates=True to respond to messages one at a time
client = TelegramClient(session_file, api_id, api_hash, sequential_updates=True)
@client.on(events.NewMessage(incoming=True))
async def handle_new_message(event):
if event.is_private: # only auto-reply to private chats
from_ = await event.client.get_entity(event.from_id)
if not from_.bot: # don't auto-reply to bots
msg = events.NewMessage(incoming=True)
if msg!='':
message= cchatbot_response(msg)
print(time.asctime(), '-', event.message) # optionally log time and message
time.sleep(1) # pause for 1 second to rate-limit automatic replies
await event.respond(message)
print(time.asctime(), '-', 'Auto-replying...')
client.start(phone, password)
client.run_until_disconnected()
解决方案
我可以运行你的代码减去这些行
message = cchatbot_response(msg)
print(time.asctime(), '-', event.message) # optionally log time and message
time.sleep(1) # pause for 1 second to rate-limit automatic replies
请检查 cchatbot_response 函数。
另外,我打电话给这条线
client.start(phone, password)
像这样
client.start()
它提示我输入电话号码和密码,但没有再次提及。
推荐阅读
- c# - 来自 C# 不完整数据的 NodeJS 发布请求
- html - 如何在输入中显示带有空格的字符串?
- firebase - 如何从firestore检查最喜欢的项目文档是否存在
- gcc - 我在 Solaris 10 SPARC 中编译的 GNU GCC 9 不工作
- python - 将项目添加到列表的副本意外行为
- python - 如何获得鼠标光标的初始位置和最终位置之间的差异?
- google-apps-script - 通过应用程序脚本中的 URL-fetch 调用 Drive REST API 版本 3 仅提供默认属性
- java - 我的文件夹中有 900 个 .pdf 格式的文件。我必须将 .pdf 一个一个转换为 .csv 并将这些 csv 存储在另一个文件夹中
- angular - Angular 8:资产未加载
- python - Excel 无法读取 XlsxWriter 创建的公式 SUMIF