luckydonald / pytg

Python package that wraps around Telegram messenger CLI. Send and receive messages, and more.
MIT License
369 stars 76 forks source link

pytg: INFO CLI did not responde #128

Open ezhevpelikan opened 6 years ago

ezhevpelikan commented 6 years ago

The problem

Hi all! Log output: ConnectionResetError: [Errno 104] Connection reset by peer 2017-12-15 10:10:06,661 pytg: INFO CLI did not responde.

Actual behaviour

Tell us what happens instead.

Your environment

ubuntu:16.04

Component Version
python 3.5.2
pytg   0.4.10  
OS     `Linux d3360e5bd9d9 4.4.0-98-generic #121-Ubuntu SMP Tue Oct 10 14:24:03 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

Steps to reproduce

Detailed steps to reproduce the issue.

# coding=utf-8

import time
import os

from pytg import Telegram
from pytg.utils import coroutine

import requests
from pytg import exceptions
from datetime import datetime
import logging
from socket import error as SocketError
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(name)s: %(levelname)s %(message)s')

def post_to_db(post):
    # do something

def main():
    home_dir = os.environ.get("TELEGRAM_HOME")
    port = os.environ.get("TELEGRAM_PORT")

    tg = Telegram(
        telegram="/tg-cli/telegram-cli",
        port = int(port),
        pubkey_file="/tg-cli/tg-server.pub",
        home_directory=home_dir)
    receiver = tg.receiver
    sender = tg.sender

    try:
        receiver.start()
    except Exception as e:
        logging.info("cannot start receiver for receive new messages: {error}".format(str(e)))
        receiver.stop()
        quit()

    try:
        receiver.message(message_func(sender))
    except SocketError as e:
        logging.info("cannot receive new messages: {error}".format(str(e)))
        receiver.stop()
        sender.safe_quit()
    except Exception as e:
        logging.info("cannot receive new messages: {error}".format(str(e)))
        receiver.stop()
        sender.safe_quit()

    receiver.stop()
    sender.safe_quit()

@coroutine
def message_func(sender):
    try:
        while True:
            msg = (yield)
            try:
                sender.status_online()
                msg = dict(msg)
                # print (msg)
                if msg.get("event") != "message":
                    continue
                if msg.get("text") is None:
                    continue
                elif "from" in msg:
                    post_to_db(post)
                elif "to" in msg:
                    post_to_db(post)
                elif "sender" in msg:
                    post_to_db(post)
                else:
                    quit()
            except exceptions.NoResponse:
                quit()
    except KeyboardInterrupt:
        quit()
    except Exception as e:
        print (e)
        logging.info("could not receive new messages: {error}".format(str(e)))
        quit()
    except GeneratorExit:
        # pass
        quit()
    else:
        # pass
        quit()

if __name__ == '__main__':
    main()
luckydonald commented 6 years ago

Heya, could you set the logging level to logging.DEBUG instead of logging.INFO, to give more information?

Also, using logging.exception("whatever") instead of print(e) will print you the stacktrace of an Exception, giving me a bit more to work with.