Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not receive voice messages #117

Open
Henning-Klatt opened this issue May 3, 2017 · 4 comments
Open

Can not receive voice messages #117

Henning-Klatt opened this issue May 3, 2017 · 4 comments

Comments

@Henning-Klatt
Copy link

Henning-Klatt commented May 3, 2017

The problem

When I send a voice message to the bot, he crashes.

My environment

Component Version
python 3.4.2
pytg   0.4.10
OS     Debian 8

Steps to reproduce

Sent a voice message

# coding=utf-8

from __future__ import unicode_literals
from pytg.receiver import Receiver
from pytg.sender import Sender
from pytg.utils import coroutine

import random

__author__ = 'luckydonald'

ADMIN_ID = "XXXXXXXX"


def main():
    receiver = Receiver(host="localhost", port=4458)
    sender = Sender(host="localhost", port=4458)

    receiver.start()
    receiver.message(get_message(sender))
    receiver.stop()

    print("Receiver stopped!")

@coroutine
def get_message(sender):
    quit = False
    try:
        while not quit:
            msg = (yield)
            sender.status_online()
            if msg.event != "message":
                continue  # is not a message.
            if msg.own:  # the bot has send this message.
                continue  # we don't want to process this message.
            if msg.text is None:  # we have media instead.
                continue  # and again, because we want to process only text message.
            if msg.text == "ping":
                sender.send_msg(msg.peer.cmd, "Pöng!")
            messages = ["hi", "hello", "hey"]
            if msg.text.lower() in messages:
                answers = ["Hi {name}", "Hey {name}", "Hello {name}", "👋"]
                sender.send_msg(msg.peer.cmd, "🤖: " + random.choice(answers).format(name=msg.peer.name))
            elif msg.text == "quit":  # you should probably check a user id
                if msg.sender.id == ADMIN_ID:
                    sender.send_msg(msg.sender.cmd, "Bye!")
                    quit = True
                else:
                    reply = "You are not my Admin.\nMy Admin has id {admin_id} but you have {user_id}".format(admin_id=ADMIN_ID, user_id=msg.sender.id)
                    sender.send_msg(msg.sender.cmd, reply)
    except GeneratorExit:
        pass
    except KeyboardInterrupt:
        pass
    else:
        pass

if __name__ == '__main__':
    main()

Expected behavior

if msg.event != "message":
Should happen and the while should be continued.

Actual behavior

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/DictObject/__init__.py", line 598, in __getattr__
    n(name))  # Raise exception if not found in original dict's attributes either
AttributeError: 'DictObject' object has no attribute 'text'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/DictObject/__init__.py", line 605, in __getattr__
    key_name = self._attribute_to_key_map[n(name)]  # Check if we have this set.
KeyError: 'text'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "auto.py", line 58, in <module>
    main()
  File "auto.py", line 18, in main
    receiver.message(get_message(sender))
  File "/usr/local/lib/python3.4/dist-packages/pytg/utils.py", line 29, in start
    cr = func(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/pytg/receiver.py", line 212, in message
    function.send(message)
  File "auto.py", line 35, in get_message
    if msg.text is None:  # we have media instead.
  File "/usr/local/lib/python3.4/dist-packages/DictObject/__init__.py", line 619, in __getattr__
    raise suppress_context(AttributeError(name))
AttributeError: text
@luckydonald
Copy link
Owner

msg.event is either any "message" type (text, photo, documents, locations, stickers, video, audio, voice, ...).
The other possible value was (If I remember right) "service" which are events like people going online or offline, group edits like renames or picture changes, and some other.

You want to check if there is "text" in the message:

if not "text" in msg and not msg.text:
    continue

@luckydonald
Copy link
Owner

Sorry I think I misread this.
Can you turn on logging (see readme, section in case of errors), and share what the CLI is actually sending?

@alanzchen
Copy link

alanzchen commented Jun 16, 2017 via email

@luckydonald
Copy link
Owner

@alanzchen There is no message there?

--
Alan Chen
Sent with Airmail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants