From 21aefeffef757a54faae64a04d7f03b700d8ef21 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Thu, 18 Jan 2024 18:18:28 +0100 Subject: [PATCH] Management: Allow to edit bot info (#4178) Co-authored-by: Alexander Zinchuk --- src/api/gramjs/apiBuilders/users.ts | 1 + src/api/gramjs/methods/bots.ts | 24 ++ src/api/gramjs/methods/index.ts | 3 +- src/api/gramjs/methods/settings.ts | 5 +- src/api/types/users.ts | 1 + src/components/right/RightHeader.tsx | 16 ++ src/components/right/management/ManageBot.tsx | 267 ++++++++++++++++++ .../right/management/Management.scss | 4 + .../right/management/Management.tsx | 10 + src/config.ts | 1 + src/global/actions/api/bots.ts | 72 ++++- src/global/actions/api/initial.ts | 14 +- src/global/helpers/users.ts | 1 + src/global/selectors/management.ts | 8 +- src/global/types.ts | 18 ++ src/lib/gramjs/tl/apiTl.js | 1 + src/lib/gramjs/tl/static/api.json | 1 + src/types/index.ts | 2 +- src/util/fallbackLangPack.ts | 5 + 19 files changed, 445 insertions(+), 9 deletions(-) create mode 100644 src/components/right/management/ManageBot.tsx diff --git a/src/api/gramjs/apiBuilders/users.ts b/src/api/gramjs/apiBuilders/users.ts index db74a705e..e4ca27ea2 100644 --- a/src/api/gramjs/apiBuilders/users.ts +++ b/src/api/gramjs/apiBuilders/users.ts @@ -71,6 +71,7 @@ export function buildApiUser(mtpUser: GramJs.TypeUser): ApiUser | undefined { type: userType, firstName, lastName, + canEditBot: Boolean(mtpUser.botCanEdit), ...(userType === 'userTypeBot' && { canBeInvitedToGroup: !mtpUser.botNochats }), ...(usernames && { usernames }), phoneNumber: mtpUser.phone || '', diff --git a/src/api/gramjs/methods/bots.ts b/src/api/gramjs/methods/bots.ts index 31a7b0d8e..5adb19501 100644 --- a/src/api/gramjs/methods/bots.ts +++ b/src/api/gramjs/methods/bots.ts @@ -564,3 +564,27 @@ function addPhotoToLocalDb(photo: GramJs.Photo) { function addWebDocumentToLocalDb(webDocument: GramJs.TypeWebDocument) { localDb.webDocuments[webDocument.url] = webDocument; } + +export function setBotInfo({ + bot, + langCode, + name, + about, + description, +}: { + bot: ApiUser; + langCode: string; + name?: string; + about?: string; + description?: string; +}) { + return invokeRequest(new GramJs.bots.SetBotInfo({ + bot: buildInputPeer(bot.id, bot.accessHash), + langCode, + name: name || '', + about: about || '', + description: description || '', + }), { + shouldReturnTrue: true, + }); +} diff --git a/src/api/gramjs/methods/index.ts b/src/api/gramjs/methods/index.ts index e47fcce3f..7a33aeb3e 100644 --- a/src/api/gramjs/methods/index.ts +++ b/src/api/gramjs/methods/index.ts @@ -75,7 +75,8 @@ export { } from './twoFaSettings'; export { - answerCallbackButton, fetchTopInlineBots, fetchInlineBot, fetchInlineBotResults, sendInlineBotResult, startBot, + answerCallbackButton, setBotInfo, fetchTopInlineBots, fetchInlineBot, fetchInlineBotResults, + sendInlineBotResult, startBot, requestWebView, requestSimpleWebView, sendWebViewData, prolongWebView, loadAttachBots, toggleAttachBot, fetchBotApp, requestBotUrlAuth, requestLinkUrlAuth, acceptBotUrlAuth, acceptLinkUrlAuth, loadAttachBot, requestAppWebView, allowBotSendMessages, fetchBotCanSendMessage, invokeWebViewCustomMethod, diff --git a/src/api/gramjs/methods/settings.ts b/src/api/gramjs/methods/settings.ts index ee9caa92a..6931335eb 100644 --- a/src/api/gramjs/methods/settings.ts +++ b/src/api/gramjs/methods/settings.ts @@ -109,9 +109,12 @@ export async function updateProfilePhoto(photo?: ApiPhoto, isFallback?: boolean) return undefined; } -export async function uploadProfilePhoto(file: File, isFallback?: boolean, isVideo = false, videoTs = 0) { +export async function uploadProfilePhoto( + file: File, isFallback?: boolean, isVideo = false, videoTs = 0, bot?: ApiUser, +) { const inputFile = await uploadFile(file); const result = await invokeRequest(new GramJs.photos.UploadProfilePhoto({ + ...(bot ? { bot: buildInputPeer(bot.id, bot.accessHash) } : undefined), ...(isVideo ? { video: inputFile, videoStartTs: videoTs } : { file: inputFile }), ...(isFallback ? { fallback: true } : undefined), })); diff --git a/src/api/types/users.ts b/src/api/types/users.ts index 41b51d8c8..d820d9678 100644 --- a/src/api/types/users.ts +++ b/src/api/types/users.ts @@ -37,6 +37,7 @@ export interface ApiUser { hasUnreadStories?: boolean; maxStoryId?: number; color?: ApiPeerColor; + canEditBot?: boolean; } export interface ApiUserFullInfo { diff --git a/src/components/right/RightHeader.tsx b/src/components/right/RightHeader.tsx index 0e74563fb..bd6e50b26 100644 --- a/src/components/right/RightHeader.tsx +++ b/src/components/right/RightHeader.tsx @@ -31,6 +31,7 @@ import useFlag from '../../hooks/useFlag'; import useLang from '../../hooks/useLang'; import useLastCallback from '../../hooks/useLastCallback'; +import Icon from '../common/Icon'; import Button from '../ui/Button'; import ConfirmDialog from '../ui/ConfirmDialog'; import SearchInput from '../ui/SearchInput'; @@ -75,6 +76,7 @@ type StateProps = { currentInviteInfo?: ApiExportedInvite; shouldSkipHistoryAnimations?: boolean; isBot?: boolean; + canEditBot?: boolean; isInsideTopic?: boolean; canEditTopic?: boolean; }; @@ -157,6 +159,7 @@ const RightHeader: FC = ({ canEditTopic, onClose, onScreenSelect, + canEditBot, }) => { const { setLocalTextSearchQuery, @@ -494,6 +497,17 @@ const RightHeader: FC = ({ )} + {canEditBot && ( + + )} {canEditTopic && (