Twilio with Android in Kotlin

Overview

Twilio is third party service that can be used to receive calls, send SMS, chat, email, IVR and many other services. This service is paid.

Add the following steps

  • Add the required dependency in build.gradle (Module) file.
 implementation "com.squareup.okhttp3:okhttp:4.9.1"
  • Create an XML File
 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/tvPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phone_number"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/edtPhone" />

<EditText
android:id="@+id/edtPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:ems="10"
android:inputType="phone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/tvPhone"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/message"
app:layout_constraintLeft_toLeftOf="@+id/tvPhone"
app:layout_constraintTop_toTopOf="@+id/edtMessage" />

<EditText
android:id="@+id/edtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textMultiLine"
app:layout_constraintLeft_toLeftOf="@+id/edtPhone"
app:layout_constraintTop_toBottomOf="@+id/edtPhone" />

<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/send"
android:textAllCaps="false"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtMessage" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • Add the following code
 class MainActivity : AppCompatActivity()
{
private lateinit var mClient : OkHttpClient
override fun onCreate(savedInstanceState : Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

try
{
btnSend.setOnClickListener {
sendSMS(applicationContext.getString(R.string.send_url) , object : Callback
{
override fun onFailure(call : Call , e : IOException)
{
e.printStackTrace()
}

override fun onResponse(call : Call , response : Response)
{
runOnUiThread {
edtPhone.setText("")
edtMessage.setText((""))
Toast.makeText(applicationContext , "SMS Sent!" , Toast.LENGTH_LONG)
.show()
}
}

})
}
} catch (e : Exception)
{
e.printStackTrace()
}
}

private fun sendSMS(url : String , callback : Callback) : Call
{
val formBody : RequestBody = FormBody.Builder().add("To" , edtPhone.text.toString())
.add("Body" , edtMessage.text.toString()).build()
val request : Request = Request.Builder().url(url).post(formBody).build()
val response : Call = mClient.newCall(request)
response.enqueue(callback)
return response
}
}

References

Comments

Popular posts from this blog

SSLSocketFactory in Android

Architecture Components in Android

DataBinding in Android