How to use the africastalking.SMS function in africastalking

To help you get started, we’ve selected a few africastalking examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github AfricasTalkingLtd / africastalking-python / test / test_sms.py View on Github external
sendPremium(message: String, keyword: String, linkId: String, recipients: List, senderId: Optional, retryDurationInHours: Optional): Send a premium SMS

fetchMessages(lastReceivedId: Optional): Fetch your messages

fetchSubscriptions(shortCode: String, keyword: String, lastReceivedId: Optional): Fetch your premium subscription data

createSubscription(shortCode: String, keyword: String, phoneNumber: String, checkoutToken: String): Create a premium subscription
"""
import africastalking
import unittest
from test import USERNAME, API_KEY

africastalking.initialize(USERNAME, API_KEY)
token_service = africastalking.Token
service = africastalking.SMS


class TestSmsService(unittest.TestCase):

    def test_send(self):
        res = service.send('test_send()', ['+254718769882', '+254718769881'], enqueue=True, sender_id='AT2FA')
        recipients = res['SMSMessageData']['Recipients']
        assert len(recipients) == 2
        assert recipients[0]['status'] == 'Success'

    
    def test_heavy_single_send(self):
        count = 100000
        phone_numbers = list(map(lambda x: str("+254718" + str(x + count)), range(1, count)))
        def on_finish(error, data):
            if (error):
github AfricasTalkingLtd / africastalking-python / example / webApp.py View on Github external
import os
from flask import Flask, request, render_template
from flask_restful import Resource, Api

import africastalking

app = Flask(__name__)
api = Api(app)

username = os.getenv('user_name', 'sandbox')
api_key = os.getenv('api_key', 'fake')


africastalking.initialize(username, api_key)
sms = africastalking.SMS
airtime  = africastalking.Airtime
payment = africastalking.Payment

@app.route('/')
def index():
    return render_template('index.html')

class send_sms(Resource):
    def get(self):
      return {'hello': 'world'}
    def post(self):
      number = str(request.form['number'])
      return sms.send("Test message", [number])
api.add_resource(send_sms, '/sms')

class send_airtime(Resource):
github AfricasTalkingLtd / africastalking-python / example / app.py View on Github external
def main():
    africastalking.initialize(os.getenv('USERNAME', 'sandbox'), os.getenv('API_KEY', 'fake'))
    sms = africastalking.SMS

    def on_finish(error, data):
        if error is not None:
            raise error

        print('\nAsync Done with -> ' + str(data['SMSMessageData']['Message']))

    # Send SMS asynchronously
    sms.send('Hello Async', ['+254718769882'], callback=on_finish)
    print('Waiting for async result....')
    # Send SMS synchronously
    result = sms.send('Hello Sync Test', ['+254718769882'])
    print('\nSync Done with -> ' + result['SMSMessageData']['Message'])