Skip to content

Instantly share code, notes, and snippets.

@muhammedfurkan
Forked from painor/call.py
Created April 25, 2020 20:21
Show Gist options
  • Save muhammedfurkan/ff599af9e99802a2070bdef0577ff150 to your computer and use it in GitHub Desktop.
Save muhammedfurkan/ff599af9e99802a2070bdef0577ff150 to your computer and use it in GitHub Desktop.
Make a call using pyrogram
#This is taken from https://github.com/LonamiWebs/Telethon-calls/blob/master/calls.py and modified
import hashlib
import os
import random
from pyrogram import Client
from pyrogram.api.functions.messages import GetDhConfig
from pyrogram.api.functions.phone import RequestCall
from pyrogram.api.types import PhoneCallProtocol
client = Client("my_account")
client.start()
def get_dh_config():
class DH:
def __init__(self, dh_config):
self.p = int.from_bytes(dh_config.p, 'big')
self.g = dh_config.g
self.resp = dh_config
return DH(client.send(GetDhConfig(0, 256)))
dh_config = get_dh_config()
class DynamicDict:
def __setattr__(self, key, value):
self.__dict__[key] = value
def get_rand_bytes(length=256):
return bytes(x ^ y for x, y in zip(
os.urandom(length), dh_config.resp.random
))
def integer_to_bytes(integer):
return int.to_bytes(
integer,
length=(integer.bit_length() + 8 - 1) // 8, # 8 bits per byte,
byteorder='big',
signed=False
)
def call_me_maybe(input_user):
PROTOCOL = PhoneCallProtocol(min_layer=93, max_layer=93, udp_p2p=True)
dhc = get_dh_config()
state = DynamicDict()
state.incoming = False
state.user_id = input_user
state.random_id = random.randint(0, 0x7fffffff - 1)
state.g = dhc.g
state.p = dhc.p
state.a = 0
while not (1 < state.a < state.p - 1):
# "A chooses a random value of a, 1 < a < p-1"
state.a = int.from_bytes(get_rand_bytes(), 'little')
state.g_a = pow(state.g, state.a, state.p)
state.g_a_hash = hashlib.sha256(integer_to_bytes(state.g_a)).digest()
state.my_proto = PROTOCOL
client.send(RequestCall(
user_id=client.resolve_peer(state.user_id),
random_id=state.random_id,
g_a_hash=state.g_a_hash,
protocol=state.my_proto))
call_me_maybe("insert the username here")
@joel122002
Copy link

Hi,
I've been trying to call via the telegram API but after multiple attempts am still not able to figure it out. I found your code but I get this error

Traceback (most recent call last):
  File "makeCall.py", line 67, in <module>
    dh_config = get_dh_config()
  File "makeCall.py", line 64, in get_dh_config
    return DH(client.send(GetDhConfig(0, 256)))
TypeError: __init__() takes 1 positional argument but 3 were given

It would be of great help if you could help me out. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment