Adding bot members to Telegram group calls using Pytgcalls and telethon

 

In this article, we will do a step by step guide on how to make a telethon script for adding bots to a telegram group call.

Installtion.

we are going to need telethon and pytgcalls libraries for this purpose.

pip install telethon

and then

pip install pytgcalls

 

Note that pytgcalls requires nodeJs and also it (pytgcalls) doesn't build on Mac M1 processors at this moment.

let's find all telethon session files in the same folder as our script is.

import the required libraries

import os
from subprocess import check_call

from telethon import TelegramClient, sync
from pytgcalls import idle
from pytgcalls import PyTgCalls
from pytgcalls import StreamType
from pytgcalls.types.input_stream import InputAudioStream
from pytgcalls.types.input_stream import InputStream

all_sessions = [x for x in os.listdir('.') if 'session' in x and 'journal' not in x]

 

and then for every session we need to start an instance of NodeJS and PyTgCalls.

client = TelegramClient(session, API_ID, API_HASH) client.connect()

we get the group entity now, later we need to use group ID for joining the group call.


group = client.get_entity(GROUP_USERNAME)
client.disconnect()

 

call_py = PyTgCalls(client)
call_py.start()

create the input audio file that is used for mocking the microphone

check_call(['touch {}'.format(input_audio_file)])

# join the call
call_py.join_group_call(
group.id,
InputStream(
InputAudioStream(
input_audio_file,
),
),
stream_type=StreamType().local_stream,
)



stay idle (so you wont be kicked out of the call by Telegram due to inactivity)


idle()

 

the full scripts:

 

import os
from subprocess import check_call

from telethon import TelegramClient, sync
from pytgcalls import idle
from pytgcalls import PyTgCalls
from pytgcalls import StreamType
from pytgcalls.types.input_stream import InputAudioStream
from pytgcalls.types.input_stream import InputStream


input_audio_file = 'input.raw'
GROUP_USERNAME = 'https://t.me/grouplink'
API_ID = 00000
API_HASH = '000000'


def work():
all_sessions = [x for x in os.listdir('.') if 'session' in x and 'journal' not in x]
for session in all_sessions:
client = TelegramClient(
session,
API_ID,
API_HASH
)
client.connect()
group = client.get_entity(GROUP_USERNAME)
client.disconnect()
call_py = PyTgCalls(client)
call_py.start()
check_call(['touch {}'.format(input_audio_file)])
call_py.join_group_call(
group.id,
InputStream(
InputAudioStream(
input_audio_file,
),
),
stream_type=StreamType().local_stream,
)
idle()


work()

0 Comments

Leave a Comment