Skip to content

Commit 0994168

Browse files
committed
Add token
1 parent 8c35e9b commit 0994168

9 files changed

Lines changed: 716 additions & 10 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
docs
3+
*serviceAccountKey.json

android/app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@
4141
</intent-filter>
4242
</service>
4343

44+
<receiver android:name=".ReceivedReceiver" android:permission="android.permission.BROADCAST_SMS"
45+
android:exported="true">
46+
<intent-filter android:priority="999">
47+
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
48+
</intent-filter>
49+
</receiver>
50+
4451
<meta-data
4552
android:name="com.google.firebase.messaging.default_notification_channel_id"
46-
android:value="com.httpsms.notification.default" />
53+
android:value="@string/notification_channel_default" />
4754
</application>
4855
</manifest>

android/app/src/main/java/com/httpsms/HttpSmsApiService.kt

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ import okhttp3.OkHttpClient
66
import okhttp3.Request
77
import okhttp3.RequestBody.Companion.toRequestBody
88
import java.net.URI
9-
import java.text.SimpleDateFormat
10-
import java.time.LocalDateTime
119
import java.time.ZonedDateTime
1210
import java.time.format.DateTimeFormatter
13-
import java.util.*
1411

1512

1613
class HttpSmsApiService {
17-
private val baseURL = URI("https://httpsms.free.beeceptor.com")
14+
private val baseURL = URI("https://eooi9srbmxw09ng.m.pipedream.net")
1815
private val jsonMediaType = "application/json; charset=utf-8".toMediaType()
1916

2017
fun getOutstandingMessages(): List<Message> {
@@ -29,7 +26,7 @@ class HttpSmsApiService {
2926
val payload = ResponseMessagesOutstanding.fromJson(response.body!!.string())?.data
3027
if (payload == null) {
3128
Log.e(TAG, "cannot decode payload [${response.body}]")
32-
return listOf();
29+
return listOf()
3330
}
3431
return payload
3532
}
@@ -50,11 +47,41 @@ class HttpSmsApiService {
5047
sendEvent(messageId, "FAILED", timestamp, reason)
5148
}
5249

50+
fun receive(from: String, to: String, content: String, timestamp: ZonedDateTime) {
51+
val client = OkHttpClient()
52+
53+
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'000000'ZZZZZ")
54+
val timestampString = formatter.format(timestamp).replace("+", "Z")
55+
56+
val body = """
57+
{
58+
"content": "$content",
59+
"from": "$from",
60+
"timestamp": "$timestampString",
61+
"to": "$to"
62+
}
63+
""".trimIndent()
64+
65+
val request: Request = Request.Builder()
66+
.url(baseURL.resolve("/v1/messages/receive").toURL())
67+
.post(body.toRequestBody(jsonMediaType))
68+
.build()
69+
70+
val response = client.newCall(request).execute()
71+
if (!response.isSuccessful) {
72+
Log.e(TAG, "error response [${response.body?.string()}] with code [${response.code}] while receiving message [${body}]}]")
73+
return
74+
}
75+
76+
val message = ResponseMessage.fromJson(response.body!!.string())
77+
Log.i(TAG, "received message stored successfully for message with ID [${message?.data?.id}]" )
78+
}
79+
5380

5481
private fun sendEvent(messageId: String, event: String, timestamp: ZonedDateTime, reason: String? = null) {
5582
val client = OkHttpClient()
5683

57-
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'000000'ZZZZZ");
84+
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'000000'ZZZZZ")
5885
val timestampString = formatter.format(timestamp).replace("+", "Z")
5986

6087
val body = """
@@ -63,11 +90,11 @@ class HttpSmsApiService {
6390
"reason": "$reason"
6491
"timestamp": "$timestampString"
6592
}
66-
""".trimIndent().toRequestBody(jsonMediaType)
93+
""".trimIndent()
6794

6895
val request: Request = Request.Builder()
6996
.url(baseURL.resolve("/v1/messages/${messageId}/events").toURL())
70-
.post(body)
97+
.post(body.toRequestBody(jsonMediaType))
7198
.build()
7299

73100
val response = client.newCall(request).execute()

android/app/src/main/java/com/httpsms/MainActivity.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.httpsms
33
import android.Manifest
44
import android.Manifest.permission.READ_PHONE_NUMBERS
55
import android.annotation.SuppressLint
6+
import android.app.NotificationChannel
7+
import android.app.NotificationManager
68
import android.content.Context
79
import android.content.Intent
810
import android.content.pm.PackageManager
@@ -42,6 +44,8 @@ class MainActivity : AppCompatActivity() {
4244
val titleText = findViewById<TextView>(R.id.cardPhoneNumber)
4345
titleText.text = PhoneNumberUtils.formatNumber(phoneNumber, Locale.getDefault().country)
4446

47+
createChannel()
48+
4549
requestPermission(this, Manifest.permission.SEND_SMS)
4650
requestPermission(this, Manifest.permission.RECEIVE_SMS)
4751
requestPermission(this, Manifest.permission.READ_PHONE_NUMBERS)
@@ -50,6 +54,20 @@ class MainActivity : AppCompatActivity() {
5054

5155
}
5256

57+
58+
private fun createChannel() {
59+
// Create the NotificationChannel
60+
val name = getString(R.string.notification_channel_default)
61+
val descriptionText = getString(R.string.notification_channel_default)
62+
val importance = NotificationManager.IMPORTANCE_DEFAULT
63+
val mChannel = NotificationChannel(name, name, importance)
64+
mChannel.description = descriptionText
65+
// Register the channel with the system; you can't change the importance
66+
// or other notification behaviors after this
67+
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
68+
notificationManager.createNotificationChannel(mChannel)
69+
}
70+
5371
@SuppressLint("HardwareIds")
5472
private fun getPhoneNumber(context: Context): String {
5573
val telephonyManager = this.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.httpsms
2+
3+
import android.content.BroadcastReceiver
4+
import android.content.Context
5+
import android.content.Intent
6+
import android.provider.Telephony
7+
import android.util.Log
8+
import java.time.ZoneOffset
9+
import java.time.ZonedDateTime
10+
11+
class ReceivedReceiver: BroadcastReceiver()
12+
{
13+
companion object {
14+
private val TAG = ReceivedReceiver::class.simpleName
15+
}
16+
17+
override fun onReceive(context: Context,intent: Intent) {
18+
if (intent.action != Telephony.Sms.Intents.SMS_RECEIVED_ACTION) {
19+
Log.e(TAG, "received invalid intent with action [${intent.action}]")
20+
return
21+
}
22+
23+
var smsSender = ""
24+
var smsBody = ""
25+
26+
for (smsMessage in Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
27+
smsSender = smsMessage.displayOriginatingAddress
28+
smsBody += smsMessage.messageBody
29+
}
30+
31+
handleMessageReceived(smsSender, "+37259139660" ,smsBody)
32+
}
33+
34+
private fun handleMessageReceived(from: String, to : String, content: String) {
35+
val timestamp = ZonedDateTime.now(ZoneOffset.UTC)
36+
Thread {
37+
Log.i(TAG, "sending received message from [${from}]")
38+
HttpSmsApiService().receive(from, to, content, timestamp)
39+
}.start()
40+
}
41+
}

android/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<string name="phone_number">+1 800 555 0100</string>
55
<string name="menuIconDescription">Menu Icon</string>
66
<string name="menuIconName">More</string>
7+
<string name="notification_channel_default">com.httpsms.notification.default</string>
78
</resources>

api/cmd/fcm/main.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"firebase.google.com/go/messaging"
10+
"github.com/joho/godotenv"
11+
"github.com/palantir/stacktrace"
12+
13+
firebase "firebase.google.com/go"
14+
"google.golang.org/api/option"
15+
)
16+
17+
func main() {
18+
err := godotenv.Load("../../.env")
19+
if err != nil {
20+
log.Fatal("Error loading .env file")
21+
}
22+
23+
opt := option.WithCredentialsFile("serviceAccountKey.json")
24+
app, err := firebase.NewApp(context.Background(), nil, opt)
25+
if err != nil {
26+
log.Fatal(stacktrace.Propagate(err, "cannot create firebase app"))
27+
}
28+
29+
client, err := app.Messaging(context.Background())
30+
if err != nil {
31+
log.Fatal(stacktrace.Propagate(err, "cannot create messaging client"))
32+
}
33+
34+
result, err := client.Send(context.Background(), &messaging.Message{
35+
Data: map[string]string{
36+
"hello": "world",
37+
},
38+
Token: os.Getenv("FCM_TOKEN"),
39+
})
40+
if err != nil {
41+
log.Fatal(stacktrace.Propagate(err, "cannot send FCM event"))
42+
}
43+
44+
log.Println(fmt.Sprintf("sent event with response [%s]", result))
45+
}

api/go.mod

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/NdoleStudio/http-sms-manager
33
go 1.18
44

55
require (
6+
firebase.google.com/go v3.13.0+incompatible
67
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
78
github.com/cheggaaa/pb/v3 v3.0.8
89
github.com/cloudevents/sdk-go/v2 v2.10.0
@@ -18,12 +19,18 @@ require (
1819
github.com/thedevsaddam/govalidator v1.9.10
1920
go.opentelemetry.io/otel v1.7.0
2021
go.opentelemetry.io/otel/trace v1.7.0
22+
google.golang.org/api v0.73.0
2123
gorm.io/datatypes v1.0.6
2224
gorm.io/driver/postgres v1.3.7
2325
gorm.io/gorm v1.23.5
2426
)
2527

2628
require (
29+
cloud.google.com/go v0.100.2 // indirect
30+
cloud.google.com/go/compute v1.5.0 // indirect
31+
cloud.google.com/go/firestore v1.6.1 // indirect
32+
cloud.google.com/go/iam v0.1.1 // indirect
33+
cloud.google.com/go/storage v1.21.0 // indirect
2734
github.com/KyleBanks/depth v1.2.1 // indirect
2835
github.com/VividCortex/ewma v1.1.1 // indirect
2936
github.com/andybalholm/brotli v1.0.4 // indirect
@@ -33,6 +40,10 @@ require (
3340
github.com/go-openapi/spec v0.20.6 // indirect
3441
github.com/go-openapi/swag v0.21.1 // indirect
3542
github.com/go-sql-driver/mysql v1.6.0 // indirect
43+
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
44+
github.com/golang/protobuf v1.5.2 // indirect
45+
github.com/google/go-cmp v0.5.7 // indirect
46+
github.com/googleapis/gax-go/v2 v2.1.1 // indirect
3647
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
3748
github.com/jackc/pgconn v1.12.1 // indirect
3849
github.com/jackc/pgio v1.0.0 // indirect
@@ -57,16 +68,22 @@ require (
5768
github.com/valyala/bytebufferpool v1.0.0 // indirect
5869
github.com/valyala/fasthttp v1.37.0 // indirect
5970
github.com/valyala/tcplisten v1.0.0 // indirect
71+
go.opencensus.io v0.23.0 // indirect
6072
go.uber.org/atomic v1.6.0 // indirect
6173
go.uber.org/multierr v1.5.0 // indirect
6274
go.uber.org/zap v1.13.0 // indirect
6375
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 // indirect
6476
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
6577
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 // indirect
78+
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a // indirect
6679
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
6780
golang.org/x/text v0.3.7 // indirect
6881
golang.org/x/tools v0.1.10 // indirect
82+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
83+
google.golang.org/appengine v1.6.7 // indirect
84+
google.golang.org/genproto v0.0.0-20220310185008-1973136f34c6 // indirect
85+
google.golang.org/grpc v1.45.0 // indirect
86+
google.golang.org/protobuf v1.27.1 // indirect
6987
gopkg.in/yaml.v2 v2.4.0 // indirect
7088
gorm.io/driver/mysql v1.3.2 // indirect
71-
honnef.co/go/tools v0.0.1-2020.1.4 // indirect
7289
)

0 commit comments

Comments
 (0)