old_contents: from datetime import datetime
  from time import sleep
  
  from django.conf import settings
  
  from django_rq import job
  
  from smsfly import SMSFlyAPI
  
  from .models import Alphaname
  
  
  DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
  
  
  @job('default')
  def addNewCampaignTask(task_id):
      sleep(0.4)
      print('Ran {}'.format(task_id))
      return 'Ran {}'.format(task_id)
  
  
  @job('high')
  def sendMessagesInstantly(message='Hi man!', send_as='Alpha',
                            description='Test task', to=('380971234567',),
                            task_id=100500):
      api = SMSFlyAPI(account_id=settings.SMS_FLY['login'],
                      account_pass=settings.SMS_FLY['password'])
      return api.send_sms_to_recipients(
          start_time=datetime.now().strftime(DATETIME_FORMAT), end_time='AUTO',
          lifetime=24, rate='AUTO', desc=description,
          source=send_as, body=message, recipients=to
      )
  
  
  @job('high')
  def submitAlphanameInstantly(name):
      api = SMSFlyAPI(account_id=settings.SMS_FLY['login'],
                      account_pass=settings.SMS_FLY['password'])
      print('Submitting register request for alphaname `{}`...'.format(name))
  
      api_response_status = api.add_alphaname(alphaname=name).state.attrs['status']
      alphaname = Alphaname.objects.get(name=name)
      alphaname.text_status = api_response_status
      alphaname.save()
  
      print('Saved')
      print('Got status `{}`...'.format(api_response_status))
  
      return api_response_status
  
new_contents: from datetime import datetime
  from itertools import starmap
  from time import sleep
  
  from django.conf import settings
  
  from django_rq import job
  
  from smsfly import SMSFlyAPI
  
  from .models import Alphaname, Task, Campaign, Follower
  
  
  DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
  
  
  @job('default')
  def addNewCampaignTask(task_id):
      sleep(0.4)
      print('Ran {}'.format(task_id))
      return 'Ran {}'.format(task_id)
  
  
  @job('high')
  def sendMessagesInstantly(message='Hi man!', send_as='Alpha',
                            description='Test task', to=('380971234567',),
                            task_id=100500):
      api = SMSFlyAPI(account_id=settings.SMS_FLY['login'],
                      account_pass=settings.SMS_FLY['password'])
      task = Task.objects.get(pk=task_id)
  
      recipients_filter = task.recipients_filter_json
      recipients = Follower.objects.get(**recipients_filter)  # Filter out recipients from external DB
  
      message_pairs = starmap(lambda r: (r.cellphone,  # Generate tuple containing cell number and message body
                                         message.format(  # substitute template tags with personalized values:
                                             firstname=r.firstname, lastname=r.lastname,
                                             middlename=r.middlename, cellphone=r.cellphone,
                                             address=r.address,
                                         )),
                              recipients)
  
      api_result = api.send_sms_pairs(
          start_time=datetime.now().strftime(DATETIME_FORMAT), end_time='AUTO',
          lifetime=24, rate='AUTO', desc=description,
          source=send_as, message_pairs=message_pairs
      )
  
      res_state = api_result.message.state
      Campaign.objects.create(task=task, code=res_state.attrs['code'],
                              datetime_sent=datetime.strptime(res_state.attrs['date'], DATETIME_FORMAT),
                              state=res_state.text, smsfly_campaign_id=int(res_state.attrs['campaignID']))
  
      return api_result
  
  
  @job('high')
  def submitAlphanameInstantly(name):
      api = SMSFlyAPI(account_id=settings.SMS_FLY['login'],
                      account_pass=settings.SMS_FLY['password'])
      print('Submitting register request for alphaname `{}`...'.format(name))
  
      api_response_status = api.add_alphaname(alphaname=name).state.attrs['status']
      alphaname = Alphaname.objects.get(name=name)
      alphaname.text_status = api_response_status
      alphaname.save()
  
      print('Saved')
      print('Got status `{}`...'.format(api_response_status))
  
      return api_response_status
  
subject: Improve campaign sending task