import multiprocessing
import pyrogram
from pyrogram import Client, MessageHandler, Filters
from pyrogram.errors.exceptions.bad_request_400 import *
from pyrogram.errors.exceptions.unauthorized_401 import *
from tgvoip import VoIPServerConfig
from tgvoip_pyrogram import VoIPFileStreamService, VoIPIncomingFileStreamCall, VoIPNativeIOService, VoIPIncomingNativeIOCall
import ffmpeg
import os
import pymysql
from DBUtils.PooledDB import PooledDB
import random
from time import sleep
import soothingsounds as ss
from pathlib import Path
from pprint import pprint
from mutagen.mp3 import MP3
def incoming():
flag = True
BASEDIR = os.path.dirname(os.path.realpath(__file__))
pool = PooledDB(creator=pymysql,
host='127.0.0.1',
user='root',
password='password',
database='tgadmin_test',
autocommit=True,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor,
blocking=False,
maxconnections=20)
db = pool.connection()
cursor = db.cursor()
sql = "SELECT * FROM tgmanager_applicationusers WHERE banned_at IS NULL ORDER BY RAND() LIMIT 1"
cursor.execute(sql)
user1 = cursor.fetchone()
proxy_lst = user1['proxy'].split(':')
# VoIPServerConfig.set_bitrate_config(80000, 100000, 60000, 5000, 5000)
client = Client(session_name='+79652230383', api_id=21724, api_hash='3e0cb5efcd52300aec5994fdfc5bdc16', ipv6=False, proxy=dict(hostname=str(proxy_lst[0]), port=int(proxy_lst[1]), username=str(proxy_lst[2]), password=str(proxy_lst[3])), app_version="0.22.0.1205-arm64-v8a", device_model='Xiaomi Mi 9', system_version='Android 10 Q (29)')
client.start()
service = VoIPNativeIOService(client, receive_calls=True)
@service.on_incoming_call
def process_call(call: VoIPIncomingNativeIOCall): # use VoIPIncomingNativeIOCall for native I/O
call.accept()
call.set_output_file(os.path.join(BASEDIR, 'incoming.raw'))
mp3 = os.listdir(os.path.join(BASEDIR, 'speech'))
item = random.choice(mp3)
print('MP3##############################', item)
pprint(item)
speech = int(item.split('/')[-1].split(".")[0])
print('SPEECH', speech)
sql = """SELECT speech FROM tgmanager_dialogues WHERE id = {id}""".format(id=speech)
cursor.execute(sql)
res = cursor.fetchone()['speech']
print(res)
stream = ffmpeg.input(os.path.join(BASEDIR, 'speech', item))
stream = ffmpeg.output(stream, os.path.join(BASEDIR, 'PCM', item.split('.')[0]+'.raw'), f='s16le', ac=1, ar=48000, acodec='pcm_s16le')
ffmpeg.run(stream)
# sleep(15)
call.play(os.path.join(BASEDIR, 'PCM', item.split('.')[0]+'.raw'))
print("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")
audio = MP3(os.path.join(BASEDIR, 'speech', item))
print('DURATION', audio.info.length)
@call.on_call_ended
def call_ended(call):
nonlocal client
nonlocal flag
client.stop()
flag = False
while flag:
pass
def outgoing():
flag = True
BASEDIR = os.path.dirname(os.path.realpath(__file__))
pool = PooledDB(creator=pymysql,
host='127.0.0.1',
user='root',
password='password',
database='tgadmin_test',
autocommit=True,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor,
blocking=False,
maxconnections=20)
db = pool.connection()
cursor = db.cursor()
sql = "SELECT * FROM tgmanager_applicationusers WHERE banned_at IS NULL ORDER BY RAND() LIMIT 1"
cursor.execute(sql)
user1 = cursor.fetchone()
proxy_lst = user1['proxy'].split(':')
# VoIPServerConfig.set_bitrate_config(80000, 100000, 60000, 5000, 5000)
client = Client(session_name=user1['phone'], api_id=user1['api_id'], api_hash=user1['api_hash'], ipv6=False, proxy=dict(hostname=str(proxy_lst[0]), port=int(proxy_lst[1]), username=str(proxy_lst[2]), password=str(proxy_lst[3])), app_version="0.22.0.1205-arm64-v8a", device_model=user1['device'], system_version=user1['os'], workdir="/home/qwentor/share/autoreg/sessions")
client.start()
service = VoIPNativeIOService(client, receive_calls=False)
call = service.start_call('@messengerN1')
@call.on_call_accepted
def on_call_accepted(call):
nonlocal client
nonlocal flag
print("###################CALL!!!!!!!!###################")
mp3 = os.listdir(os.path.join(BASEDIR, 'speech'))
item = random.choice(mp3)
print('MP3##############################', item)
pprint(item)
speech = int(item.split('/')[-1].split(".")[0])
print('SPEECH', speech)
sql = """SELECT speech FROM tgmanager_dialogues WHERE id = {id}""".format(id=speech)
cursor.execute(sql)
res = cursor.fetchone()['speech']
print(res)
stream = ffmpeg.input(os.path.join(BASEDIR, 'speech', item))
print("S1")
stream = ffmpeg.output(stream, os.path.join(BASEDIR, 'PCM', item.split('.')[0]+'.raw'), f='s16le', ac=1, ar=48000, acodec='pcm_s16le')
ffmpeg.run(stream)
print("S2")
sleep(5)
print("S3")
call.play(os.path.join(BASEDIR, 'PCM', item.split('.')[0]+'.raw'))
print("S4")
call.set_output_file(os.path.join(BASEDIR, 'outgoing.raw'))
print("S5")
audio = MP3(os.path.join(BASEDIR, 'speech', item))
print('DURATION', audio.info.length)
# call.stop()
# print("Call STOPPED!!!!!!!!!!!!!")
# flag = False
@call.on_call_ended
def call_ended(call):
nonlocal client
nonlocal flag
client.stop()
print("Client STOPPED!!!!!!!!!!!!!")
flag = False
while flag:
pass
proc1 = multiprocessing.Process(target=incoming, name='incoming')
proc1.start()
proc2 = multiprocessing.Process(target=outgoing, args=(), name='outgoing')
proc2.start()