Send and receive SMS messages using your Android phone programmatically via a simple HTTP API
Add heartbeat monitor
Acho Arnoldebde11bed5f0
diff --git a/api/pkg/emails/hermes_user_email_factory.go b/api/pkg/emails/hermes_user_email_factory.goindex f8971e81..6f00c375 100644--- a/api/pkg/emails/hermes_user_email_factory.go+++ b/api/pkg/emails/hermes_user_email_factory.go@@ -120,6 +120,17 @@ func (factory *hermesUserEmailFactory) PhoneDead(user *entities.User, lastHeartbfmt.Sprintf("We haven't received any heartbeat event from your mobile phone %s since %s.", owner, lastHeartbeatTimestamp.Format(time.RFC1123)),fmt.Sprintf("Check if the mobile phone is powered on and if it has stable internet connection."),},+ Actions: []hermes.Action{+ {+ Instructions: "Click the button below to upgrade your plan and continue sending more messages",+ Button: hermes.Button{+ Color: "#329ef4",+ TextColor: "#FFFFFF",+ Text: "HEARTBEATS",+ Link: fmt.Sprintf("https://httpsms.com/heartbeats/%s", owner),+ },+ },+ },Title: "Hey,",Signature: "Cheers",Outros: []string{diff --git a/api/pkg/entities/user.go b/api/pkg/entities/user.goindex becfd5c6..2e8a53c7 100644--- a/api/pkg/entities/user.go+++ b/api/pkg/entities/user.go@@ -37,6 +37,7 @@ type User struct {ID UserID `json:"id" gorm:"primaryKey;type:string;" example:"WB7DRDWrJZRGbYrv2CKGkqbzvqdC"`Email string `json:"email" example:"[email protected]"` // gorm:"uniqueIndex"APIKey string `json:"api_key" example:"xyz"` // gorm:"uniqueIndex"+ Timezone string `json:"timezone" example:"Europe/Helsinki" gorm:"default:Africa/Accra"`ActivePhoneID *uuid.UUID `json:"active_phone_id" gorm:"type:uuid;" example:"32343a19-da5e-4b1b-a767-3298a73703cb"`SubscriptionName SubscriptionName `json:"subscription_name" example:"free"`SubscriptionID *string `json:"subscription_id" example:"8f9c71b8-b84e-4417-8408-a62274f65a08"`diff --git a/web/components/MessageThreadHeader.vue b/web/components/MessageThreadHeader.vueindex 480ac951..44b583a6 100644--- a/web/components/MessageThreadHeader.vue+++ b/web/components/MessageThreadHeader.vue@@ -45,6 +45,10 @@<v-btnx-smallv-bind="attrs"+ :to="{+ name: 'heartbeats-id',+ params: { id: $store.getters.getOwner },+ }"color="success"class="ml-2 mt-1 mb-n1"icondiff --git a/web/nuxt.config.js b/web/nuxt.config.jsindex 0ca71ee4..4a6e2b3d 100644--- a/web/nuxt.config.js+++ b/web/nuxt.config.js@@ -57,7 +57,11 @@ export default {css: [],// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins- plugins: ['~plugins/filters.ts', { src: '~/plugins/vue-glow', ssr: false }],+ plugins: [+ '~plugins/filters.ts',+ { src: '~/plugins/vue-glow', ssr: false },+ { src: '~/plugins/chart', ssr: false },+ ],// Auto import components: https://go.nuxtjs.dev/config-componentscomponents: true,@@ -143,7 +147,9 @@ export default {},// Build Configuration: https://go.nuxtjs.dev/config-build- build: {},+ build: {+ transpile: ['chart.js', 'vue-chartjs'],+ },server: {port: 30000, // default: 3000diff --git a/web/package.json b/web/package.jsonindex bd909663..dc9fa1b4 100644--- a/web/package.json+++ b/web/package.json@@ -26,15 +26,19 @@"@nuxtjs/dotenv": "^1.4.1","@nuxtjs/firebase": "^8.2.2","@nuxtjs/sitemap": "^2.4.0",+ "chart.js": "^4.3.0",+ "chartjs-adapter-moment": "^1.0.1","core-js": "^3.30.2","date-fns": "^2.30.0","dotenv": "^16.0.3","firebase": "^9.22.0","firebaseui": "^6.0.2","libphonenumber-js": "^1.10.30",+ "moment": "^2.29.4","nuxt": "^2.16.3","nuxt-highlightjs": "^1.0.3","vue": "^2.6.14",+ "vue-chartjs": "^5.2.0","vue-class-component": "^7.2.6","vue-glow": "^1.4.2","vue-property-decorator": "^9.1.2",diff --git a/web/pages/heartbeats/_id.vue b/web/pages/heartbeats/_id.vuenew file mode 100644index 00000000..ba9d87a0--- /dev/null+++ b/web/pages/heartbeats/_id.vue@@ -0,0 +1,250 @@+<template>+ <v-container fluid class="pa-0" :fill-height="$vuetify.breakpoint.lgAndUp">+ <div class="w-full h-full">+ <v-app-bar height="60" :dense="$vuetify.breakpoint.mdAndDown" fixed>+ <v-btn icon to="/threads">+ <v-icon>{{ mdiArrowLeft }}</v-icon>+ </v-btn>+ <v-toolbar-title+ >Heartbeats+ <v-icon x-small class="mx-2" color="primary">{{ mdiCircle }}</v-icon>+ <span v-if="$store.getters.getOwner">{{+ $store.getters.getOwner | phoneNumber+ }}</span></v-toolbar-title+ >+ </v-app-bar>+ <v-container class="mt-16">+ <v-row>+ <v-col cols="12">+ <p>+ Every 15 minutes, the httpSMS app on your Android phone sends a+ heartbeat event to the httpsms API to show that it is alive. The+ reason for this is because the Android operating system sometimes+ kills an application to save battery+ <a+ href="https://dontkillmyapp.com"+ class="text-decoration-none"+ target="_blank"+ >https://dontkillmyapp.com</a+ >.+ </p>+ <p>+ If httpSMS doesn't get any heartbeat event in a 1-hour interval,+ you will get an email notification about it so you can check if+ there is an issue with your Android phone.+ </p>+ </v-col>+ <v-col v-if="$vuetify.breakpoint.mdAndUp" cols="12" class="px-0">+ <bar-chart :data="chartData" :options="chartOptions"></bar-chart>+ </v-col>+ <v-col cols="12">+ <p>+ The table below shows the last 100 heartbeat events received from+ the httpSMS app on your Android phone.+ </p>+ <v-data-table+ :value="selected"+ hide-default-footer+ :headers="dataTableHeaders"+ :items="dataTableItems"+ sort-by="timestamp"+ sort-desc+ :items-per-page="100"+ class="heartbeat--table"+ >+ <template #item.interval="{ item }">+ {{ formatDuration(item.interval) }}+ </template>+ <template #item.owner="{ item }">+ {{ item.owner | phoneNumber }}+ </template>+ <template #item.timestamp="{ item }">+ {{ item.timestamp | timestamp }}+ </template>+ </v-data-table>+ </v-col>+ </v-row>+ </v-container>+ </div>+ </v-container>+</template>++<script>+import { mdiArrowLeft, mdiCircle } from '@mdi/js'+import 'chartjs-adapter-moment'+import { formatDuration, intervalToDuration } from 'date-fns'+import vueClassComponentEsm from 'vue-class-component'++export default {+ name: 'HeartbeatIndex',+ middleware: ['auth'],++ data() {+ return {+ mdiArrowLeft,+ mdiCircle,+ heartbeats: [],+ selected: [3, 6],+ dataTableHeaders: [+ {+ text: 'HEARTBEAT ID',+ align: 'start',+ sortable: false,+ value: 'id',+ },+ { text: 'PHONE NUMBER', value: 'owner', sortable: false },+ { text: 'RECEIVED AT', value: 'timestamp' },+ { text: 'TIME INTERVAL', value: 'interval' },+ ],+ }+ },++ head() {+ return {+ title: 'Heartbeats - Http SMS',+ }+ },++ computed: {+ vueClassComponentEsm() {+ return vueClassComponentEsm+ },+ dataTableItems() {+ return this.heartbeats.map((heartbeat, index) => {+ let interval = 0+ if (index < 99) {+ interval = this.getDiff(+ heartbeat.timestamp,+ this.heartbeats[index + 1].timestamp+ )+ }+ const item = {+ id: heartbeat.id,+ timestamp: heartbeat.timestamp,+ owner: heartbeat.owner,+ interval,+ }+ if (interval > 3600000) {+ this.selected.push(item)+ }+ return item+ })+ },+ chartOptions() {+ const minDate = new Date()+ minDate.setDate(minDate.getDate() - 1)+ return {+ responsive: true,+ maintainAspectRatio: false,+ plugins: {+ legend: {+ display: false,+ },+ tooltip: {+ callbacks: {+ label: function (context) {+ if (context.dataIndex === 99) {+ return '-'+ }+ const duration = intervalToDuration({+ start: new Date(+ context.dataset.data[context.dataIndex + 1].x+ ),+ end: new Date(context.dataset.data[context.dataIndex].x),+ })+ return formatDuration(duration)+ },+ },+ },+ },+ scales: {+ x: {+ type: 'time',+ },+ y: {+ display: false,+ },+ },+ }+ },+ chartData() {+ const data = this.heartbeats.map((heartbeat) => {+ return {+ x: new Date(heartbeat.timestamp).toISOString(),+ y: 1,+ }+ })++ if (!data.length) {+ return {+ datasets: [+ {+ data,+ backgroundColor: '#2196f3',+ },+ ],+ }+ }++ let prev = new Date(data[0].x)+ const newData = []+ for (let i = 1; i < data.length; i++) {+ const current = new Date(data[i].x)+ const diff = prev - current+ if (diff > 600000) {+ // 10 minutes+ newData.push(data[i])+ prev = current+ }+ }++ return {+ datasets: [+ {+ data: newData,+ backgroundColor: '#2196f3',+ },+ ],+ }+ },+ },++ async mounted() {+ await this.$store.dispatch('loadUser')+ await this.$store.dispatch('loadPhones')+ this.getHeartbeat()+ },++ methods: {+ getDiff(a, b) {+ return new Date(a) - new Date(b)+ },++ formatDuration(duration) {+ if (duration === 0) {+ return '-'+ }+ const start = new Date()+ start.setMilliseconds(start.getMilliseconds() + duration)+ return (+ formatDuration(intervalToDuration({ start: new Date(), end: start })) ||+ '0 seconds'+ )+ },++ getHeartbeat() {+ this.$store.dispatch('getHeartbeat', 100).then((heartbeats) => {+ this.heartbeats = heartbeats+ })+ },+ },+}+</script>++<style lang="scss">+.v-application {+ .heartbeat--table.v-data-table tbody tr.v-data-table__selected {+ background: #b71c1c;+ }+}+</style>diff --git a/web/pages/messages/index.vue b/web/pages/messages/index.vueindex fd697dac..0e544117 100644--- a/web/pages/messages/index.vue+++ b/web/pages/messages/index.vue@@ -1,13 +1,17 @@<template><v-container fluid class="pa-0" :fill-height="$vuetify.breakpoint.lgAndUp"><div class="w-full h-full">- <v-app-bar height="60" :dense="$vuetify.breakpoint.mdAndDown">+ <v-app-bar height="60" :dense="$vuetify.breakpoint.mdAndDown" fixed><v-btn icon to="/threads"><v-icon>{{ mdiArrowLeft }}</v-icon></v-btn>- <v-toolbar-title>New Message</v-toolbar-title>+ <v-toolbar-title+ >New Message+ <v-icon x-small class="mx-2" color="primary">{{ mdiCircle }}</v-icon>+ {{ $store.getters.getOwner | phoneNumber }}</v-toolbar-title+ ></v-app-bar>- <v-container>+ <v-container class="mt-16"><v-row><v-col cols="12" md="8" offset-md="2" xl="6" offset-xl="3"><v-form @submit.prevent="sendMessage">@@ -64,7 +68,7 @@</template><script>-import { mdiArrowLeft, mdiSend, mdiSim } from '@mdi/js'+import { mdiArrowLeft, mdiSend, mdiSim, mdiCircle } from '@mdi/js'import axios from '@/plugins/axios'export default {@@ -74,6 +78,7 @@ export default {return {mdiArrowLeft,mdiSend,+ mdiCircle,mdiSim,simOptions: [{ title: 'Default', code: 'DEFAULT' },diff --git a/web/pages/settings/index.vue b/web/pages/settings/index.vueindex 43d98f8c..38a4c1e8 100644--- a/web/pages/settings/index.vue+++ b/web/pages/settings/index.vue@@ -5,7 +5,7 @@:fill-height="$vuetify.breakpoint.lgAndUp"><div class="w-full h-full">- <v-app-bar height="60" :dense="$vuetify.breakpoint.mdAndDown">+ <v-app-bar height="60" fixed :dense="$vuetify.breakpoint.mdAndDown"><v-btn icon to="/threads"><v-icon>{{ mdiArrowLeft }}</v-icon></v-btn>@@ -13,7 +13,7 @@<div class="py-16">Settings</div></v-toolbar-title></v-app-bar>- <v-container>+ <v-container class="mt-16"><v-row><v-col cols="12" md="9" offset-md="1" xl="8" offset-xl="2"><div v-if="$fire.auth.currentUser" class="text-center">diff --git a/web/plugins/chart.ts b/web/plugins/chart.tsnew file mode 100644index 00000000..a5ad4a71--- /dev/null+++ b/web/plugins/chart.ts@@ -0,0 +1,34 @@+import Vue from 'vue'+import { Bar } from 'vue-chartjs'+import {+ Chart as ChartJS,+ Title,+ Tooltip,+ Legend,+ BarElement,+ CategoryScale,+ LinearScale,+ TimeSeriesScale,+ LineElement,+ PointElement,+ ArcElement,+ TimeScale,+} from 'chart.js'++ChartJS.register(+ Title,+ Tooltip,+ Legend,+ PointElement,+ BarElement,+ TimeScale,+ TimeSeriesScale,+ CategoryScale,+ LinearScale,+ LineElement,+ ArcElement+)++Vue.component('BarChart', {+ extends: Bar,+})diff --git a/web/store/index.ts b/web/store/index.tsindex cde75977..24c39458 100644--- a/web/store/index.ts+++ b/web/store/index.ts@@ -388,20 +388,36 @@ export const actions = {await context.commit('setAxiosError', error)},- async getHeartbeat(context: ActionContext<State, State>) {- const response = await axios.get('/v1/heartbeats', {- params: {- limit: 1,- owner: context.getters.getOwner,- },+ getHeartbeat(+ context: ActionContext<State, State>,+ limit = 1+ ): Promise<Array<Heartbeat>> {+ return new Promise<Array<Heartbeat>>((resolve, reject) => {+ axios+ .get('/v1/heartbeats', {+ params: {+ limit,+ owner: context.getters.getOwner,+ },+ })+ .then((response: AxiosResponse) => {+ if (response.data.data.length > 0) {+ context.commit('setHeartbeat', response.data.data[0])+ } else {+ context.commit('setHeartbeat', null)+ }+ resolve(response.data.data)+ })+ .catch(async (error: AxiosError) => {+ await context.dispatch('addNotification', {+ message:+ error.response?.data?.message ??+ 'Errors while fetching heartbeat',+ type: 'error',+ })+ reject(error)+ })})-- 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) {diff --git a/web/yarn.lock b/web/yarn.lockindex c47941cd..32fc9e16 100644--- a/web/yarn.lock+++ b/web/yarn.lock@@ -2242,6 +2242,11 @@"@jridgewell/resolve-uri" "3.1.0""@jridgewell/sourcemap-codec" "1.4.14"+"@kurkle/color@^0.3.0":+ version "0.3.2"+ resolved "https://registry.yarnpkg.com/@kurkle/color/-/color-0.3.2.tgz#5acd38242e8bde4f9986e7913c8fdf49d3aa199f"+ integrity sha512-fuscdXJ9G1qb7W8VdHi+IwRqij3lBkosAm4ydQtEmbY58OzHXqQhvlxqEkoz0yssNVn38bcpRWgA9PP+OGoisw==+"@mdi/js@^7.2.96":version "7.2.96"resolved "https://registry.npmjs.org/@mdi/js/-/js-7.2.96.tgz"@@ -4894,6 +4899,18 @@ chardet@^0.7.0:resolved "https://registry.npmjs.org/chardet/-/chardet-0.7.0.tgz"integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==+chart.js@^4.3.0:+ version "4.3.0"+ resolved "https://registry.yarnpkg.com/chart.js/-/chart.js-4.3.0.tgz#ac363030ab3fec572850d2d872956f32a46326a1"+ integrity sha512-ynG0E79xGfMaV2xAHdbhwiPLczxnNNnasrmPEXriXsPJGjmhOBYzFVEsB65w2qMDz+CaBJJuJD0inE/ab/h36g==+ dependencies:+ "@kurkle/color" "^0.3.0"++chartjs-adapter-moment@^1.0.1:+ version "1.0.1"+ resolved "https://registry.yarnpkg.com/chartjs-adapter-moment/-/chartjs-adapter-moment-1.0.1.tgz#0f04c30d330b207c14bfb57dfaae9ce332f09102"+ integrity sha512-Uz+nTX/GxocuqXpGylxK19YG4R3OSVf8326D+HwSTsNw1LgzyIGRo+Qujwro1wy6X+soNSnfj5t2vZ+r6EaDmA==+"chokidar@>=3.0.0 <4.0.0", chokidar@^3.4.1, chokidar@^3.4.2, chokidar@^3.5.2, chokidar@^3.5.3:version "3.5.3"resolved "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz"@@ -9886,6 +9903,11 @@ mlly@^1.1.0, mlly@^1.1.1:pkg-types "^1.0.2"ufo "^1.1.1"+moment@^2.29.4:+ version "2.29.4"+ resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108"+ integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==+move-concurrently@^1.0.1:version "1.0.1"resolved "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz"@@ -13582,6 +13604,11 @@ vm-browserify@^1.0.1:resolved "https://registry.npmjs.org/vm-browserify/-/vm-browserify-1.1.2.tgz"integrity sha512-2ham8XPWTONajOR0ohOKOHXkm3+gaBmGut3SRuu75xLd/RRaY6vqgh8NBYYk7+RW3u5AtzPQZG8F10LHkl0lAQ==+vue-chartjs@^5.2.0:+ version "5.2.0"+ resolved "https://registry.yarnpkg.com/vue-chartjs/-/vue-chartjs-5.2.0.tgz#3d0076ccf8016d1bf8fab5ccd837e7fb81005ded"+ integrity sha512-d3zpKmGZr2OWHQ1xmxBcAn5ShTG917+/UCLaSpaCDDqT0U7DBsvFzTs69ZnHCgKoXT55GZDW8YEj9Av+dlONLA==+vue-class-component@^7.2.6:version "7.2.6"resolved "https://registry.npmjs.org/vue-class-component/-/vue-class-component-7.2.6.tgz"