def main(): load_config() init_logging() loop = asyncio.get_event_loop() helga = Helga(loop=loop) def ask_exit(signal_name): logging.info("Got signal {}: exiting…".format(signal_name)) helga.shutdown() for signal_name in ('SIGINT', 'SIGTERM'): loop.add_signal_handler(getattr(signal, signal_name), functools.partial(ask_exit, signal_name)) # TODO: Add signal handler for SIGHUP (reload config) try: loop.run_until_complete(helga.get_updates()) finally: loop.close()
'date': datetime.datetime.fromtimestamp(config.getint('message', 'origtime')), 'file': os.path.join(spool_path, wav_file)}) return messages def encode_opus(filename): args = ['opusenc', '--quiet', filename, '-'] with open(filename, 'rb') as ih: proc = subprocess.Popen(args=args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (out, err) = proc.communicate() if err or len(out) == 0: print('error', err) opus_data = BytesIO(out) # convice aiohttp to set the correct header opus_data.name = 'voicemail.opus' return opus_data if __name__ == '__main__': load_config() init_logging() loop = asyncio.get_event_loop() helga = Helga(loop=loop) messages = [] with shelve.open(os.path.join(config.workdir, 'chat.shelve')) as s: for mail in get_voicemail(): cmd = SendMessage(chat_id=s['teddydestodes'], text='New Voicemail from {caller} at {date}'.format(caller=mail['from'], date=str(mail['date']))) messages.append(helga._execute_command(cmd)) cmd = SendVoice(chat_id=s['teddydestodes'], voice=encode_opus(mail['file'])) messages.append(helga._execute_command(cmd, headers={'content-type': 'audio/opus'})) loop.run_until_complete(asyncio.gather(*messages))