import axios from 'axios'; import { Field, Form, Formik } from 'formik'; import React from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { useToasts } from 'react-toast-notifications'; import useSWR from 'swr'; import * as Yup from 'yup'; import globalMessages from '../../../i18n/globalMessages'; import Alert from '../../Common/Alert'; import Button from '../../Common/Button'; import LoadingSpinner from '../../Common/LoadingSpinner'; import NotificationTypeSelector from '../../NotificationTypeSelector'; const messages = defineMessages({ agentenabled: 'Enable Agent', botUsername: 'Bot Username', botUsernameTip: 'Allow users to start a chat with the bot and configure their own personal notifications', botAPI: 'Bot Authentication Token', chatId: 'Chat ID', validationBotAPIRequired: 'You must provide a bot authentication token', validationChatIdRequired: 'You must provide a valid chat ID', telegramsettingssaved: 'Telegram notification settings saved successfully!', telegramsettingsfailed: 'Telegram notification settings failed to save.', testsent: 'Telegram test notification sent!', settinguptelegram: 'Setting Up Telegram Notifications', settinguptelegramDescription: 'To configure Telegram notifications, you will need to create a bot and get the bot API key. Additionally, you will need the chat ID for the chat to which you would like to send notifications. You can find this by adding @get_id_bot to the chat and issuing the /my_id command.', sendSilently: 'Send Silently', sendSilentlyTip: 'Send notifications with no sound', }); const NotificationsTelegram: React.FC = () => { const intl = useIntl(); const { addToast } = useToasts(); const { data, error, revalidate } = useSWR( '/api/v1/settings/notifications/telegram' ); const NotificationsTelegramSchema = Yup.object().shape({ botAPI: Yup.string().when('enabled', { is: true, then: Yup.string() .nullable() .required(intl.formatMessage(messages.validationBotAPIRequired)), otherwise: Yup.string().nullable(), }), chatId: Yup.string() .when('enabled', { is: true, then: Yup.string() .nullable() .required(intl.formatMessage(messages.validationChatIdRequired)), otherwise: Yup.string().nullable(), }) .matches( /^-?\d+$/, intl.formatMessage(messages.validationChatIdRequired) ), }); if (!data && !error) { return ; } return ( { try { await axios.post('/api/v1/settings/notifications/telegram', { enabled: values.enabled, types: values.types, options: { botAPI: values.botAPI, chatId: values.chatId, sendSilently: values.sendSilently, botUsername: values.botUsername, }, }); addToast(intl.formatMessage(messages.telegramsettingssaved), { appearance: 'success', autoDismiss: true, }); } catch (e) { addToast(intl.formatMessage(messages.telegramsettingsfailed), { appearance: 'error', autoDismiss: true, }); } finally { revalidate(); } }} > {({ errors, touched, isSubmitting, values, isValid, setFieldValue }) => { const testSettings = async () => { await axios.post('/api/v1/settings/notifications/telegram/test', { enabled: true, types: values.types, options: { botAPI: values.botAPI, chatId: values.chatId, sendSilently: values.sendSilently, botUsername: values.botUsername, }, }); addToast(intl.formatMessage(messages.testsent), { appearance: 'info', autoDismiss: true, }); }; return ( <> {intl.formatMessage(messages.settinguptelegramDescription, { CreateBotLink: function CreateBotLink(msg) { return ( {msg} ); }, GetIdBotLink: function GetIdBotLink(msg) { return ( {msg} ); }, code: function code(msg) { return {msg}; }, })}
{errors.botUsername && touched.botUsername && (
{errors.botUsername}
)}
{errors.botAPI && touched.botAPI && (
{errors.botAPI}
)}
{errors.chatId && touched.chatId && (
{errors.chatId}
)}
setFieldValue('types', newTypes)} />
); }}
); }; export default NotificationsTelegram;