Search code examples
pythoncommand-line-interfacepexpect

Python pexpect.expect() on Telegram CLI


I'm trying to make a bot for Telegram on my RPi but I'm pretty new to this. I installed Telegram Cli and pexpect.

This is my test.py file:

import pexpect

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

However this give me the following error:

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.

I couldnt find any good documentation about the expect function. Basically what I am trying to do is send X when someone messages me something containing Y. But I can't get the first thing working. Simply sending a command.


Solution

  • Here's a Pexpect tutorial, and some sample code:

    source

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

    output

    3