import React from 'react'; import { Field, Form, Formik } from 'formik'; import useSWR from 'swr'; import LoadingSpinner from '../../Common/LoadingSpinner'; import Button from '../../Common/Button'; import { defineMessages, useIntl } from 'react-intl'; import axios from 'axios'; import * as Yup from 'yup'; import { useToasts } from 'react-toast-notifications'; import Alert from '../../Common/Alert'; import NotificationTypeSelector from '../../NotificationTypeSelector'; const messages = defineMessages({ save: 'Save Changes', saving: 'Saving…', agentenabled: 'Enable Agent', botAPI: 'Bot API', chatId: 'Chat ID', validationBotAPIRequired: 'You must provide a Bot API key.', validationChatIdRequired: 'You must provide a Chat ID.', telegramsettingssaved: 'Telegram notification settings saved!', telegramsettingsfailed: 'Telegram notification settings failed to save.', testsent: 'Test notification sent!', test: 'Test', settinguptelegram: 'Setting Up Telegram Notifications', settinguptelegramDescription: 'To setup Telegram you need to create a bot and get the bot API key.\ Additionally, you need the chat ID for the chat you want the bot to send notifications to.\ You can do this by adding @get_id_bot to the chat or group chat.', notificationtypes: 'Notification Types', }); 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().required( intl.formatMessage(messages.validationBotAPIRequired) ), chatId: Yup.string().required( 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, }, }); 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, }, }); 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} ); }, })}
{errors.botAPI && touched.botAPI && (
{errors.botAPI}
)}
{errors.chatId && touched.chatId && (
{errors.chatId}
)}
{intl.formatMessage(messages.notificationtypes)}
setFieldValue('types', newTypes) } />
); }}
); }; export default NotificationsTelegram;