Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Setup pollers

Acho Arnold55ccee519a34
diff --git a/api/pkg/handlers/heartbeat_handler.go b/api/pkg/handlers/heartbeat_handler.go
index a1bc5710..babd920b 100644
--- a/api/pkg/handlers/heartbeat_handler.go
+++ b/api/pkg/handlers/heartbeat_handler.go
@@ -38,7 +38,7 @@ func NewHeartbeatHandler(
// RegisterRoutes registers the routes for the MessageHandler
func (h *HeartbeatHandler) RegisterRoutes(router fiber.Router) {
- router.Get("/heartbeat", h.Index)
+ router.Get("/heartbeats", h.Index)
}
// Index returns the heartbeats of a phone number
diff --git a/web/components/MessageThreadHeader.vue b/web/components/MessageThreadHeader.vue
index f3dd50d1..9e5dcec8 100644
--- a/web/components/MessageThreadHeader.vue
+++ b/web/components/MessageThreadHeader.vue
@@ -8,9 +8,37 @@
<nuxt-link to="/" class="text-decoration-none text--primary">
<v-toolbar-title>
{{ $store.getters.getOwner | phoneNumber }}
+ <v-progress-circular
+ v-if="$store.getters.getPolling"
+ indeterminate
+ :size="14"
+ :width="1"
+ class="mt-n1"
+ color="success"
+ ></v-progress-circular>
</v-toolbar-title>
</nuxt-link>
- <p class="text--secondary mb-n1">Estonia</p>
+ <div class="d-flex">
+ <p class="text--secondary mb-n1">
+ {{ $store.getters.getOwner | phoneCountry }}
+ </p>
+ <v-tooltip v-if="$store.getters.getHeartbeat" right>
+ <template #activator="{ on, attrs }">
+ <v-btn
+ x-small
+ v-bind="attrs"
+ color="success"
+ class="ml-2 mt-1 mb-n1"
+ icon
+ v-on="on"
+ >
+ <v-icon x-small>mdi-circle</v-icon>
+ </v-btn>
+ </template>
+ <h4>Last Heartbeat</h4>
+ {{ $store.getters.getHeartbeat.timestamp | timestamp }}
+ </v-tooltip>
+ </div>
</div>
<v-spacer></v-spacer>
<v-tooltip bottom>
diff --git a/web/layouts/default.vue b/web/layouts/default.vue
index b824ec03..14c7e7ac 100644
--- a/web/layouts/default.vue
+++ b/web/layouts/default.vue
@@ -29,7 +29,39 @@ import { Vue, Component } from 'vue-property-decorator'
@Component
export default class DefaultLayout extends Vue {
mounted() {
- this.$store.dispatch('loadThreads')
+ Promise.all([
+ this.$store.dispatch('loadThreads'),
+ this.$store.dispatch('getHeartbeat'),
+ ])
+ this.startPoller()
+ }
+
+ startPoller() {
+ setInterval(async () => {
+ await this.$store.dispatch('setPolling', true)
+
+ const promises = []
+ if (this.$store.getters.getOwner) {
+ promises.push(
+ this.$store.dispatch('loadThreads'),
+ this.$store.dispatch('getHeartbeat')
+ )
+ }
+
+ if (this.$store.getters.hasThread) {
+ promises.push(
+ this.$store.dispatch(
+ 'loadThreadMessages',
+ this.$store.getters.getThread.id
+ )
+ )
+ }
+ await Promise.all(promises)
+
+ setTimeout(() => {
+ this.$store.dispatch('setPolling', false)
+ }, 1000)
+ }, 10000)
}
}
</script>
diff --git a/web/models/heartbeat.ts b/web/models/heartbeat.ts
new file mode 100644
index 00000000..db1b6e9a
--- /dev/null
+++ b/web/models/heartbeat.ts
@@ -0,0 +1,6 @@
+export interface Heartbeat {
+ id: string
+ owner: string
+ timestamp: string
+ quantity: string
+}
diff --git a/web/plugins/filters.ts b/web/plugins/filters.ts
index bca8079f..c5156670 100644
--- a/web/plugins/filters.ts
+++ b/web/plugins/filters.ts
@@ -8,3 +8,16 @@ Vue.filter('phoneNumber', (value: string): string => {
}
return value
})
+
+Vue.filter('phoneCountry', (value: string): string => {
+ const phoneNumber = parsePhoneNumber(value)
+ if (phoneNumber && phoneNumber.country) {
+ const regionNames = new Intl.DisplayNames(undefined, { type: 'region' })
+ return regionNames.of(phoneNumber.country) ?? 'earth'
+ }
+ return 'Earth'
+})
+
+Vue.filter('timestamp', (value: string): string => {
+ return new Date(value).toLocaleString()
+})
diff --git a/web/store/index.ts b/web/store/index.ts
index bf4ec9c9..05c18853 100644
--- a/web/store/index.ts
+++ b/web/store/index.ts
@@ -2,17 +2,23 @@ import { ActionContext } from 'vuex'
import axios from '~/plugins/axios'
import { MessageThread } from '~/models/message-thread'
import { Message } from '~/models/message'
+import { Heartbeat } from '~/models/heartbeat'
+import { compile } from 'vue-template-compiler'
type State = {
owner: string
threads: Array<MessageThread>
threadId: string | null
+ heartbeat: null | Heartbeat
+ pooling: boolean
threadMessages: Array<Message>
}
export const state = (): State => ({
threads: [],
threadId: null,
+ heartbeat: null,
+ pooling: false,
threadMessages: [],
owner: '+37259139660',
})
@@ -40,6 +46,14 @@ export const getters = {
}
return thread
},
+
+ getHeartbeat(state: State): Heartbeat | null {
+ return state.heartbeat
+ },
+
+ getPolling(state: State): boolean {
+ return state.pooling
+ },
}
export const mutations = {
@@ -52,6 +66,12 @@ export const mutations = {
setThreadMessages(state: State, payload: Array<Message>) {
state.threadMessages = payload
},
+ setHeartbeat(state: State, payload: Heartbeat | null) {
+ state.heartbeat = payload
+ },
+ setPooling(state: State, payload: boolean) {
+ state.pooling = payload
+ },
}
export type SendMessageRequest = {
@@ -70,6 +90,26 @@ export const actions = {
context.commit('setThreads', response.data.data)
},
+ async getHeartbeat(context: ActionContext<State, State>) {
+ const response = await axios.get('/v1/heartbeats', {
+ params: {
+ limit: 1,
+ owner: context.getters.getOwner,
+ },
+ })
+
+ if (response.data.data.length > 0) {
+ context.commit('setHeartbeat', response.data.data[0])
+ return
+ }
+
+ context.commit('setHeartbeat', null)
+ },
+
+ setPolling(context: ActionContext<State, State>, status: boolean) {
+ context.commit('setPooling', status)
+ },
+
async sendMessage(
context: ActionContext<State, State>,
request: SendMessageRequest