import { BeakerIcon, SaveIcon } from '@heroicons/react/outline'; import axios from 'axios'; import { Field, Form, Formik } from 'formik'; import React, { useState } 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 Button from '../../Common/Button'; import LoadingSpinner from '../../Common/LoadingSpinner'; import SensitiveInput from '../../Common/SensitiveInput'; import NotificationTypeSelector from '../../NotificationTypeSelector'; const messages = defineMessages({ agentenabled: 'Enable Agent', botUsername: 'Bot Username', botUsernameTip: 'Allow users to also start a chat with your bot and configure their own notifications', botAPI: 'Bot Authorization Token', botApiTip: 'Create a bot for use with Overseerr', chatId: 'Chat ID', chatIdTip: 'Start a chat with your bot, add @get_id_bot, and issue the /my_id command', validationBotAPIRequired: 'You must provide a bot authorization token', validationChatIdRequired: 'You must provide a valid chat ID', telegramsettingssaved: 'Telegram notification settings saved successfully!', telegramsettingsfailed: 'Telegram notification settings failed to save.', toastTelegramTestSending: 'Sending Telegram test notification…', toastTelegramTestSuccess: 'Telegram test notification sent!', toastTelegramTestFailed: 'Telegram test notification failed to send.', sendSilently: 'Send Silently', sendSilentlyTip: 'Send notifications with no sound', }); const NotificationsTelegram: React.FC = () => { const intl = useIntl(); const { addToast, removeToast } = useToasts(); const [isTesting, setIsTesting] = useState(false); const { data, error, mutate: 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', 'types'], { is: (enabled: boolean, types: number) => enabled && !!types, 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, setFieldTouched, }) => { const testSettings = async () => { setIsTesting(true); let toastId: string | undefined; try { addToast( intl.formatMessage(messages.toastTelegramTestSending), { autoDismiss: false, appearance: 'info', }, (id) => { toastId = id; } ); 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, }, }); if (toastId) { removeToast(toastId); } addToast(intl.formatMessage(messages.toastTelegramTestSuccess), { autoDismiss: true, appearance: 'success', }); } catch (e) { if (toastId) { removeToast(toastId); } addToast(intl.formatMessage(messages.toastTelegramTestFailed), { autoDismiss: true, appearance: 'error', }); } finally { setIsTesting(false); } }; return (
{errors.botAPI && touched.botAPI && (
{errors.botAPI}
)}
{errors.botUsername && touched.botUsername && (
{errors.botUsername}
)}
{errors.chatId && touched.chatId && (
{errors.chatId}
)}
{ setFieldValue('types', newTypes); setFieldTouched('types'); if (newTypes) { setFieldValue('enabled', true); } }} error={ errors.types && touched.types ? (errors.types as string) : undefined } />
); }}
); }; export default NotificationsTelegram;