diff --git a/telemovris/telemovris.py b/telemovris/telemovris.py index 6127537..9964685 100644 --- a/telemovris/telemovris.py +++ b/telemovris/telemovris.py @@ -63,37 +63,39 @@ async def main(user, message_text, audio): async with client: audio_name, audio_duration = generate_audio(message_text) - logging.info(f"Calling {user}") - call = await voip_service.start_call(user) + logging.info(f"Sending message to: {user}") await client.send_message(user, message_text) + try: if audio == 'yes': + logging.info(f"Calling {user}") + call = await voip_service.start_call(user) call.play(f"/data/audio/{audio_name}.raw") + call_has_ended = False + + @call.on_call_state_changed + def state_changed(call, state): + print(f'State changed: {state}') + + @call.on_call_ended + async def call_ended(call): + global call_has_ended + call_has_ended = True + + t1 = time.perf_counter() + while True: + t2 = time.perf_counter() + await asyncio.sleep(1) + try: + if call.ctrl.call_duration > audio_duration or call_has_ended or (call.state == CallState.WAITING and int(t2-t1) > min(30, RING_WAIT_TIME)) or call.ctrl.call_duration > MAX_CALL_DURATION: + logging.info("Finishing call because of duration") + await call.discard_call() + break + except Exception as e: + logging.error(f"There was an error during the call: {e}") + break except Exception as e: logging.error(f"Error: {e}") - call_has_ended = False - - @call.on_call_state_changed - def state_changed(call, state): - print(f'State changed: {state}') - - @call.on_call_ended - async def call_ended(call): - global call_has_ended - call_has_ended = True - - t1 = time.perf_counter() - while True: - t2 = time.perf_counter() - await asyncio.sleep(1) - try: - if call.ctrl.call_duration > audio_duration or call_has_ended or (call.state == CallState.WAITING and int(t2-t1) > min(30, RING_WAIT_TIME)) or call.ctrl.call_duration > MAX_CALL_DURATION: - logging.info("Finishing call because of duration") - await call.discard_call() - break - except Exception as e: - logging.error(f"There was an error during the call: {e}") - break while True: time.sleep(5)