Telegram WebRTC (VoIP)

This project consists of two main parts: tgcalls, pytgcalls. The first is a C++ Python extension. The second uses the extension along with MTProto and provides high level SDK. All together, it allows you to create userbots that can record and broadcast in voice chats, make and receive private calls.

Pyrogram's snippet

from pyrogram import Client, filters
from pyrogram.utils import MAX_CHANNEL_ID

from pytgcalls import GroupCallFactory

app = Client('pytgcalls')
group_call = GroupCallFactory(app).get_file_group_call('input.raw')


@group_call.on_network_status_changed
async def on_network_changed(context, is_connected):
    chat_id = MAX_CHANNEL_ID - context.full_chat.id
    if is_connected:
        await app.send_message(chat_id, 'Successfully joined!')
    else:
        await app.send_message(chat_id, 'Disconnected from voice chat..')


@app.on_message(filters.outgoing & filters.command('join'))
async def join_handler(_, message):
    await group_call.start(message.chat.id)


app.run()

Telethon's snippet

from telethon import TelegramClient, events

from pytgcalls import GroupCallFactory

app = TelegramClient('pytgcalls', api_id, api_hash).start()
group_call_factory = GroupCallFactory(app, GroupCallFactory.MTPROTO_CLIENT_TYPE.TELETHON)
group_call = group_call_factory.get_file_group_call('input.raw')


@app.on(events.NewMessage(outgoing=True, pattern=r'^/join$'))
async def join_handler(event):
    chat = await event.get_chat()
    await group_call.start(chat.id)

app.run_until_disconnected()

Features

  • Python solution.
  • Supporting popular MTProto libraries: Pyrogram, Telethon.
  • Abstract class to implement own MTProto bridge.
  • Work with voice chats in channels and chats.
  • Multiply voice chats (example).
  • System of custom handlers on events.
  • Join as channels or chats.
  • Join using invite (speaker) links.
  • Speaking status with audio levels inside and outside of voice chat.
  • Mute/unmute, pause/resume, stop/play, volume control and more...

Available sources of input/output data transfers

Note: All audio data is transmitted in PCM 16 bit, 48k.
Example how to convert files using FFmpeg.

Requirements

  • Python 3.6 or higher.
  • A Telegram API key.
  • x86_64/arm64 platform and Unix system (use WSL for Windows).

TODO list

  • Incoming and Outgoing calls (already there and working, but not in release).
  • Private and group video calls.
  • Python binary wheels for Windows
    and more...

Installing

For Pyrogram

pip3 install -U pytgcalls[pyrogram]

For Telethon

pip3 install -U pytgcalls[telethon]

GitHub

https://github.com/MarshalX/tgcalls