async def add(self, name, email, phone, listId): pushData = { 'api_key': self.api_key, 'fields[email]': email, 'list_ids': listId, 'double_optin': 3, 'format': self.format } if name: pushData['fields[Name]'] = name if phone: pushData['fields[phone]'] = '+' + str(phone) async with aiohttp.ClientSession() as session: async with session.post(self.url, data=pushData) as resp: if not resp.status == 200: log_error('Unisender не отвечает!') return data = await resp.json() if 'error' in data: text = 'Не получилось записать email {}\nОшибка: {}' log_error(text.format(email, data['error'])) else: log_info('Email {} записан'.format(email))
def __init__(self, log, pid, config): thisDir = getcwd() + '/' log_init(thisDir+log) with open(thisDir+pid, 'w') as f: f.write(str(getpid())) try: with open(thisDir+config) as file: self.config = yaml.safe_load(file) except FileNotFoundError: log_error('Нет конфигурационного файла')
async def send_msg(self, msgText): url = self.urlStart + 'sendMessage' sendData = {'chat_id': self.chat, 'text': msgText} try: async with aiohttp.ClientSession() as session: async with session.get(url, params=sendData) as resp: data = await resp.json() if not data['ok']: text = 'Ошибка {} при отправке Telegram-сообщения {}.' log_error(text.format(data['description'], msgText)) return else: log_info('Отправлено Telegram-сообщение {}.'.format( msgText)) return except aiohttp.ClientConnectorError as e: log_error('Ошибка подключения к API Telegram.') return
async def __send(self, msgData): try: async with aiohttp.ClientSession() as session: async with session.get(self.url, params=msgData) as resp: pass except aiohttp.ServerDisconnectedError as resp: if not resp.message.code == 200: log_error('GoIP не отвечает!') return if 'ERROR' in resp.message.reason: text = 'Не получилось отправить смс {} на номер {}' text += '\nОшибка: {}' log_error( text.format(msgData['m'], msgData['p'], resp.message.reason)) else: log_info('Смс {} на номер {} отправлено'.format( msgData['m'], msgData['p']))
async def send_loop(app): while True: try: await asyncio.sleep(1) nextMsg = await app['db'].get_next() nextSms = await app['db'].get_next_sms() if not nextMsg and not nextSms: continue now = datetime.now(gettz('Europe/Moscow')) if nextMsg: if now >= nextMsg['time']: try: await app['api'].add( nextMsg['name'], nextMsg['email'], nextMsg['phone'], nextMsg['list']) except: pass else: await app['db'].remove_entry(nextMsg['id']) if nextSms: if now >= nextSms['time']: try: await app['sms'].send_random_sim( nextSms['text'], nextSms['phone']) except Exception as e: pass else: await app['db'].remove_sms(nextSms['id']) except asyncio.CancelledError: break except Exception as e: log_error('Неожиданная ошибка сервера: '+str(e)) break