Python pexpect.expect() 在 Telegram CLI 上的用法

0 投票
1 回答
1064 浏览
提问于 2025-04-18 18:22

我正在尝试在我的树莓派上为Telegram制作一个机器人,但我对这个还很陌生。我安装了Telegram Cli和pexpect库。

这是我的test.py文件:

import pexpect

telegram = pexpect.spawn('./telegram -k tg.pub')
telegram.expect("User")
telegram.send("msg Big_Boss test")

但是我遇到了以下错误:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    telegram.expect("User*")
  File "/usr/local/lib/python2.7/dist-packages/pexpect/__init__.py", line 1451, in expect
    timeout, searchwindowsize)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/__init__.py", line 1466, in expect_list
    timeout, searchwindowsize)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/__init__.py", line 1554, in expect_loop
    raise EOF(str(err) + '\n' + str(self))
pexpect.EOF: End Of File (EOF). Exception style platform.

我找不到关于expect函数的好文档。基本上,我想做的是当有人给我发包含Y的消息时,发送X。但是我无法让第一个步骤正常工作。也就是简单地发送一个命令。

1 个回答

1

这里有一个关于 Pexpect 模块的教程,还有一些示例代码:

源代码

import pexpect

calc = pexpect.spawn('bc')
calc.expect("details")
print calc.send("1+2")

输出结果

3

撰写回答