Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Add VoIP support (WIP) #218

Closed
wants to merge 10 commits into from
Next Next commit
Add tgvoip support (WIP)
  • Loading branch information
bakatrouble committed Feb 15, 2019
commit cf8edbed3df60e7d4f26fecebb1e85c95e72150a
5 changes: 3 additions & 2 deletions pyrogram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 39,11 @@
UserProfilePhotos, Venue, Animation, Video, VideoNote, Voice, CallbackQuery, Messages, ForceReply,
InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove,
Poll, PollOption, ChatPreview, StopPropagation, ContinuePropagation, Game, CallbackGame, GameHighScore,
GameHighScores
GameHighScores,
BaseCall, FileCallMixin, IncomingCall, IncomingFileCall, OutgoingCall, OutgoingFileCall
)
from .client import (
Client, ChatAction, ParseMode, Emoji,
MessageHandler, DeletedMessagesHandler, CallbackQueryHandler,
RawUpdateHandler, DisconnectHandler, UserStatusHandler, Filters
RawUpdateHandler, DisconnectHandler, UserStatusHandler, PhoneCallHandler, Filters
)
3 changes: 2 additions & 1 deletion pyrogram/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 22,6 @@
from .handlers import (
MessageHandler, DeletedMessagesHandler,
CallbackQueryHandler, RawUpdateHandler,
DisconnectHandler, UserStatusHandler
DisconnectHandler, UserStatusHandler,
PhoneCallHandler
)
23 changes: 21 additions & 2 deletions pyrogram/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 48,7 @@
VolumeLocNotFound, UserMigrate, FileIdInvalid, ChannelPrivate, PhoneNumberOccupied,
PasswordRecoveryNa, PasswordEmpty
)
from pyrogram.client.handlers import DisconnectHandler
from pyrogram.client.handlers import DisconnectHandler, PhoneCallHandler
from pyrogram.client.handlers.handler import Handler
from pyrogram.client.methods.password.utils import compute_check
from pyrogram.crypto import AES
Expand Down Expand Up @@ -199,7 199,8 @@ def __init__(self,
config_file: str = BaseClient.CONFIG_FILE,
plugins: dict = None,
no_updates: bool = None,
takeout: bool = None):
takeout: bool = None,
receive_phone_calls: bool = False):
super().__init__()

self.session_name = session_name
Expand All @@ -226,6 227,7 @@ def __init__(self,
self.plugins = plugins
self.no_updates = no_updates
self.takeout = takeout
self.receive_phone_calls = receive_phone_calls

self.dispatcher = Dispatcher(self, workers)

Expand Down Expand Up @@ -336,6 338,20 @@ def start(self):
mimetypes.init()
Syncer.add(self)

if self.receive_phone_calls:
def _(client, call):
from pyrogram import ContinuePropagation, StopPropagation
if isinstance(call, types.PhoneCallRequested):
from pyrogram.client.types import IncomingFileCall
incoming_call = IncomingFileCall(client, call)
self.calls.append(incoming_call)
for handler in self.incoming_call_handlers:
callable(handler) and handler(client, incoming_call)
raise StopPropagation
raise ContinuePropagation

self.add_handler(PhoneCallHandler(_))

return self

def stop(self):
Expand Down Expand Up @@ -376,6 392,9 @@ def stop(self):

self.media_sessions.clear()

for i in self.calls:
i.discard()

self.is_started = False
self.session.stop()

Expand Down
8 changes: 6 additions & 2 deletions pyrogram/client/dispatcher/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 24,8 @@

import pyrogram
from pyrogram.api import types
from ..handlers import CallbackQueryHandler, MessageHandler, RawUpdateHandler, UserStatusHandler, DeletedMessagesHandler
from ..handlers import CallbackQueryHandler, MessageHandler, RawUpdateHandler, UserStatusHandler, \
DeletedMessagesHandler, PhoneCallHandler

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -73,7 74,10 @@ def __init__(self, client, workers: int):
(types.UpdateUserStatus,):
lambda upd, usr, cht: (
pyrogram.UserStatus._parse(self.client, upd.status, upd.user_id), UserStatusHandler
)
),

(types.UpdatePhoneCall,):
lambda upd, usr, cht: (upd.phone_call, PhoneCallHandler),
}

self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple}
Expand Down
3 changes: 3 additions & 0 deletions pyrogram/client/ext/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 98,9 @@ def __init__(self):
self.download_queue = Queue()
self.download_workers_list = []

self.incoming_call_handlers = []
self.calls = []

self.disconnect_handler = None

def send(self, *args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions pyrogram/client/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 22,4 @@
from .message_handler import MessageHandler
from .raw_update_handler import RawUpdateHandler
from .user_status_handler import UserStatusHandler
from .phone_call_handler import PhoneCallHandler
24 changes: 24 additions & 0 deletions pyrogram/client/handlers/phone_call_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 1,24 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from .handler import Handler


class PhoneCallHandler(Handler):
def __init__(self, callback: callable, filters=None):
super().__init__(callback, filters)
4 changes: 3 additions & 1 deletion pyrogram/client/methods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 23,7 @@
from .messages import Messages
from .password import Password
from .users import Users
from .voip import VoIP


class Methods(
Expand All @@ -32,6 33,7 @@ class Methods(
Chats,
Users,
Messages,
Decorators
Decorators,
VoIP
):
pass
4 changes: 3 additions & 1 deletion pyrogram/client/methods/decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 22,7 @@
from .on_message import OnMessage
from .on_raw_update import OnRawUpdate
from .on_user_status import OnUserStatus
from .on_incoming_call import OnIncomingCall


class Decorators(
Expand All @@ -30,6 31,7 @@ class Decorators(
OnCallbackQuery,
OnRawUpdate,
OnDisconnect,
OnUserStatus
OnUserStatus,
OnIncomingCall,
):
pass
25 changes: 25 additions & 0 deletions pyrogram/client/methods/decorators/on_incoming_call.py
Original file line number Diff line number Diff line change
@@ -0,0 1,25 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from ...ext import BaseClient


class OnIncomingCall(BaseClient):
def on_incoming_call(self, func: callable) -> callable:
self.incoming_call_handlers.append(func)
return func
6 changes: 6 additions & 0 deletions pyrogram/client/methods/voip/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 1,6 @@
from .start_call import StartCall

class VoIP(
StartCall
):
pass
30 changes: 30 additions & 0 deletions pyrogram/client/methods/voip/start_call.py
Original file line number Diff line number Diff line change
@@ -0,0 1,30 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from typing import Union

import pyrogram
from ...ext import BaseClient


class StartCall(BaseClient):
def start_call(self,
user_id: Union[int, str]) -> "pyrogram.OutgoingFileCall":
call = pyrogram.OutgoingFileCall(client=self, user_id=user_id)
self.calls.append(call)
return call
7 changes: 7 additions & 0 deletions pyrogram/client/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 35,10 @@
Chat, ChatMember, ChatMembers, ChatPhoto,
Dialog, Dialogs, User, UserStatus, ChatPreview
)

try:
from .voip import (
IncomingCall, IncomingFileCall, OutgoingCall, OutgoingFileCall, BaseCall, FileCallMixin
)
except ImportError:
pass
6 changes: 6 additions & 0 deletions pyrogram/client/types/voip/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 1,6 @@
from .base_call import BaseCall
from .incoming_call import IncomingCall
from .outgoing_call import OutgoingCall
from .file_call_mixin import FileCallMixin
from .incoming_file_call import IncomingFileCall
from .outgoing_file_call import OutgoingFileCall
Loading