uptime-kuma/src/components/notifications/Telegram.vue

89 lines
3 KiB
Vue
Raw Normal View History

2021-09-07 15:32:05 +00:00
<template>
<div class="mb-3">
2021-09-21 05:02:41 +00:00
<label for="telegram-bot-token" class="form-label">{{ $t("Bot Token") }}</label>
<HiddenInput id="telegram-bot-token" v-model="$parent.notification.telegramBotToken" :required="true" autocomplete="one-time-code"></HiddenInput>
<div class="form-text">
2021-09-21 05:02:41 +00:00
{{ $t("You can get a token from") }} <a href="https://t.me/BotFather" target="_blank">https://t.me/BotFather</a>.
2021-09-07 15:32:05 +00:00
</div>
</div>
2021-09-07 15:32:05 +00:00
<div class="mb-3">
2021-09-21 05:02:41 +00:00
<label for="telegram-chat-id" class="form-label">{{ $t("Chat ID") }}</label>
2021-09-07 15:32:05 +00:00
<div class="input-group mb-3">
<input id="telegram-chat-id" v-model="$parent.notification.telegramChatID" type="text" class="form-control" required>
<button v-if="$parent.notification.telegramBotToken" class="btn btn-outline-secondary" type="button" @click="autoGetTelegramChatID">
{{ $t("Auto Get") }}
</button>
</div>
2021-09-07 15:32:05 +00:00
<div class="form-text">
2021-09-21 05:02:41 +00:00
{{ $t("supportTelegramChatID") }}
2021-09-07 15:32:05 +00:00
<p style="margin-top: 8px;">
2021-09-21 05:02:41 +00:00
{{ $t("wayToGetTelegramChatID") }}
</p>
2021-09-07 15:32:05 +00:00
<p style="margin-top: 8px;">
<template v-if="$parent.notification.telegramBotToken">
<a :href="telegramGetUpdatesURL" target="_blank" style="word-break: break-word;">{{ telegramGetUpdatesURL }}</a>
</template>
2021-09-07 15:32:05 +00:00
<template v-else>
{{ telegramGetUpdatesURL }}
</template>
</p>
2021-09-07 15:32:05 +00:00
</div>
</div>
2021-09-07 15:32:05 +00:00
</template>
<script>
import HiddenInput from "../HiddenInput.vue";
import axios from "axios";
import { useToast } from "vue-toastification"
const toast = useToast();
export default {
components: {
HiddenInput,
},
computed: {
telegramGetUpdatesURL() {
2021-09-21 05:02:41 +00:00
let token = `<${this.$t("YOUR BOT TOKEN HERE")}>`
2021-09-07 15:32:05 +00:00
if (this.$parent.notification.telegramBotToken) {
token = this.$parent.notification.telegramBotToken;
}
return `https://api.telegram.org/bot${token}/getUpdates`;
},
},
methods: {
async autoGetTelegramChatID() {
try {
let res = await axios.get(this.telegramGetUpdatesURL)
if (res.data.result.length >= 1) {
let update = res.data.result[res.data.result.length - 1]
if (update.channel_post) {
this.notification.telegramChatID = update.channel_post.chat.id;
} else if (update.message) {
this.notification.telegramChatID = update.message.chat.id;
} else {
2021-09-21 05:02:41 +00:00
throw new Error(this.$t("chatIDNotFound"))
2021-09-07 15:32:05 +00:00
}
} else {
2021-09-21 05:02:41 +00:00
throw new Error(this.$t("chatIDNotFound"))
2021-09-07 15:32:05 +00:00
}
} catch (error) {
toast.error(error.message)
}
},
}
}
</script>