Programmer Level

Setting up and getting started.

The africasTalking client library is required to run the examples below.

Simply install ‘AfricasTalkingGateway’ via nuget.

  • Using visual studio click the ‘Tools’ menu bar and select ‘Nuget Package Manager’ > ‘Package Manager Console’
  • a new window called ‘package manager console ‘ will appear.
  • Run this command to install the api wrapper ‘install-package AfricasTalkingGateway’.

1. Beginner

SMS

Sending an SMS in vb.Net

Class MainClass

Public Shared Sub Main()

' Specify your login credentials

Dim username As String = "AT_Username"

Dim apiKey As String = "AT_APIKey"

Dim recipients As String = "+254711XXXYYY,+254733YYYZZZ"

Dim message As String = "I'm a lumberjack, I sleep all night and I work all day"

Dim gateway As New AfricasTalkingGateway(username, apiKey)

Try

Dim results As dynamic = gateway.sendMessage(recipients, message)

For Each result As dynamic In results

Console.Write(DirectCast(result("number"), String) & Convert.ToString(","))

Console.Write(DirectCast(result("status"), String) & Convert.ToString(","))

Console.Write(DirectCast(result("messageId"), String) & Convert.ToString(","))

Console.WriteLine(DirectCast(result("cost"), String))

Next

Catch e As AfricasTalkingGatewayException

Console.WriteLine("Encountered an error: " + e.Message)

End Try

End Sub

End Class

AIRTIME

Sending Airtime

Sending airtime in VB

Imports System.IO

Imports System.Collections

Imports System.Collections.Generic

Public Class Application

Public Shared Sub Main()

Dim airtimeRecipientsList As New List(Of Hashtable)()

Dim recipient1 As New Hashtable()

recipient1("phoneNumber") = "+254711XXXYYY"

recipient1("amount") = "KES XX"

airtimeRecipientsList.add(recipient1)

Dim recipient2 As New Hashtable()

recipient2("phoneNumber") = "+254733YYYZZZ"

recipient2("amount") = "KES YY"

airtimeRecipientsList.add(recipient2)

Dim gateway As New AfricasTalkingGateway("username", "APIKey")

Try

gateway.sendAirtime(airtimeRecipientsList)

catch ex As AfricasTalkingGatewayException

System.Console.WriteLine(ex.Message())

End Try

End Sub

End Class

VOICE

Calling a number from a virtual phone number. Make sure you have raised a number on the sandbox <here>

Imports System.IO

Imports System.Collections

Imports System.Collections.Generic

Public Class Application

Public Shared Sub Main()

Dim gateway As New AfricasTalkingGateway("username", "APIKey")

gateway.call("+25470XXXXXX", "+254711XXXYYY")

End Sub

End Class

USSD

A few things to note about USSD:

  • USSD is session driven. Every request we send you will contain a sessionId, and this will be maintained until that session is completed
  • You will need to let the Mobile Service Provider know whether the session is complete or not. If the session is ongoing, please begin your response with CON. If this is the last response for that session, begin your response with END.
  • If we get a HTTP error response (Code 40X) from your script, or a malformed response (does not begin with CON or END, we will terminate the USSD session gracefully.
Parameter Method Description
sessionId POST This is a session unique value generated when the session starts and sent every time a mobile subscriber response has been received
phoneNumber POST This is the mobile subscriber number
serviceCode POST This is your ussd code
text POST This is the user input

using visual studio 2013 or Greater

The full project is on github

  • go to file > new > project
  • in the projects window choose web under visual basic
  • select ASP.NET Web Application and name your project as you like
  • click next
  • in the template window select Empty and ensure the web api checkbox is selected and click ok
  • after your project has been set up, locate the Controllers folder in your solution explorer
  • right click on the Controllers folder, select Add > Controller
  • in the Add Scaffold window, select Web API 2 Controller - Empty and click add
  • Give it a name, we will call it MobileController
  • paste this code in the MobileController.vb file.

Imports System.NetImports System.Net.HttpImports System.Web.HttpNamespace Controllers Public Class ServerResponse Public Property text() As String Get Return m_text End Get Set m_text = Value End Set End Property Private m_text As String Public Property phoneNumber() As String Get Return m_phoneNumber End Get Set m_phoneNumber = Value End Set End Property Private m_phoneNumber As String Public Property sessionId() As String Get Return m_sessionId End Get Set m_sessionId = Value End Set End Property Private m_sessionId As String Public Property serviceCode() As String Get Return m_serviceCode End Get Set m_serviceCode = Value End Set End Property Private m_serviceCode As String End Class<RoutePrefix("api/mobile")>

Public Class MobileController Inherits ApiController <Route("ussd")>

<HttpPost, ActionName("ussd")>

Public Function ussd(<FromBody> ServerResponse As ServerResponse) As HttpResponseMessage Dim rs As HttpResponseMessage Dim response As String If ServerResponse.text Is Nothing Then ServerResponse.text = "" End If

If ServerResponse.text.Equals("", StringComparison.Ordinal) Then response = "CON This is AfricasTalking \n" +

"1. Get your phone number"ElseIf ServerResponse.text.Equals("1", StringComparison.Ordinal) Then response = "END Your phone number is " + ServerResponse.phoneNumber Else response = "END invalid option"End If rs = Request.CreateResponse(HttpStatusCode.Created, response) rs.Content = New StringContent(response, Encoding.UTF8, "text/plain") Return rsEnd Function

End Class

End Namespace

  1. Build and run your project.
  2. When a user dials your ussd code, the AfricasTalking server will send a post request to your registered callback url. Use any localhost tunneling tool like ‘ngrok’.

results matching ""

    No results matching ""