Skip to content

Commit

Permalink
Web app improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
laktyushin committed Sep 2, 2023
1 parent 4236d30 commit 882d362
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 6 deletions.
11 changes: 11 additions & 0 deletions Telegram/Telegram-iOS/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9854,3 +9854,14 @@ Sorry for the inconvenience.";
"Location.TypeCity" = "City";
"Location.TypeStreet" = "Street";
"Location.TypeLocation" = "Location";

"WebApp.AllowWriteTitle" = "Allow Sending Messages?";
"WebApp.AllowWriteConfirmation" = "This will allow the bot **%@** to message you on Telegram.";

"AuthSessions.MessageApp" = "You allowed this bot to message you when you opened %@.";
"Notification.BotWriteAllowedMenu" = "You allowed this bot to message you when you added it to your attachment menu.";
"Notification.BotWriteAllowedRequest" = "You allowed this bot to message you in the app.";

"WebApp.SharePhoneTitle" = "Share Phone Number?";
"WebApp.SharePhoneConfirmation" = "**%@** will know your phone number. This can be useful for integration with other services.";
"WebApp.SharePhoneConfirmationUnblock" = "**%@** will know your phone number. This can be useful for integration with other services.\n\nThis will also unblock the bot.";
47 changes: 47 additions & 0 deletions submodules/TelegramApi/Sources/Api31.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,21 @@ public extension Api.functions.auth {
})
}
}
public extension Api.functions.bots {
static func allowSendMessage(bot: Api.InputUser) -> (FunctionDescription, Buffer, DeserializeFunctionResponse<Api.Updates>) {
let buffer = Buffer()
buffer.appendInt32(-248323089)
bot.serialize(buffer, true)
return (FunctionDescription(name: "bots.allowSendMessage", parameters: [("bot", String(describing: bot))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.Updates? in
let reader = BufferReader(buffer)
var result: Api.Updates?
if let signature = reader.readInt32() {
result = Api.parse(reader, signature: signature) as? Api.Updates
}
return result
})
}
}
public extension Api.functions.bots {
static func answerWebhookJSONQuery(queryId: Int64, data: Api.DataJSON) -> (FunctionDescription, Buffer, DeserializeFunctionResponse<Api.Bool>) {
let buffer = Buffer()
Expand All @@ -1822,6 +1837,21 @@ public extension Api.functions.bots {
})
}
}
public extension Api.functions.bots {
static func canSendMessage(bot: Api.InputUser) -> (FunctionDescription, Buffer, DeserializeFunctionResponse<Api.Bool>) {
let buffer = Buffer()
buffer.appendInt32(324662502)
bot.serialize(buffer, true)
return (FunctionDescription(name: "bots.canSendMessage", parameters: [("bot", String(describing: bot))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.Bool? in
let reader = BufferReader(buffer)
var result: Api.Bool?
if let signature = reader.readInt32() {
result = Api.parse(reader, signature: signature) as? Api.Bool
}
return result
})
}
}
public extension Api.functions.bots {
static func getBotCommands(scope: Api.BotCommandScope, langCode: String) -> (FunctionDescription, Buffer, DeserializeFunctionResponse<[Api.BotCommand]>) {
let buffer = Buffer()
Expand Down Expand Up @@ -1870,6 +1900,23 @@ public extension Api.functions.bots {
})
}
}
public extension Api.functions.bots {
static func invokeWebViewCustomMethod(bot: Api.InputUser, customMethod: String, params: Api.DataJSON) -> (FunctionDescription, Buffer, DeserializeFunctionResponse<Api.DataJSON>) {
let buffer = Buffer()
buffer.appendInt32(142591463)
bot.serialize(buffer, true)
serializeString(customMethod, buffer: buffer, boxed: false)
params.serialize(buffer, true)
return (FunctionDescription(name: "bots.invokeWebViewCustomMethod", parameters: [("bot", String(describing: bot)), ("customMethod", String(describing: customMethod)), ("params", String(describing: params))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.DataJSON? in
let reader = BufferReader(buffer)
var result: Api.DataJSON?
if let signature = reader.readInt32() {
result = Api.parse(reader, signature: signature) as? Api.DataJSON
}
return result
})
}
}
public extension Api.functions.bots {
static func reorderUsernames(bot: Api.InputUser, order: [String]) -> (FunctionDescription, Buffer, DeserializeFunctionResponse<Api.Bool>) {
let buffer = Buffer()
Expand Down
19 changes: 17 additions & 2 deletions submodules/TelegramCore/Sources/ApiUtils/TelegramMediaAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,23 @@ func telegramMediaActionFromApiAction(_ action: Api.MessageAction) -> TelegramMe
return TelegramMediaAction(action: .historyScreenshot)
case let .messageActionCustomAction(message):
return TelegramMediaAction(action: .customText(text: message, entities: []))
case let .messageActionBotAllowed(_, domain, _):
return TelegramMediaAction(action: .botDomainAccessGranted(domain: domain ?? ""))
case let .messageActionBotAllowed(flags, domain, app):
if let domain = domain {
return TelegramMediaAction(action: .botDomainAccessGranted(domain: domain))
} else {
var appName: String?
if case let .botApp(_, _, _, _, appNameValue, _, _, _, _) = app {
appName = appNameValue
}
var type: BotSendMessageAccessGrantedType?
if (flags & (1 << 1)) != 0 {
type = .attachMenu
}
if (flags & (1 << 3)) != 0 {
type = .request
}
return TelegramMediaAction(action: .botAppAccessGranted(appName: appName, type: type))
}
case .messageActionSecureValuesSentMe:
return nil
case let .messageActionSecureValuesSent(types):
Expand Down
2 changes: 1 addition & 1 deletion submodules/TelegramCore/Sources/State/Serialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public class BoxedMessage: NSObject {

public class Serialization: NSObject, MTSerialization {
public func currentLayer() -> UInt {
return 161
return 162
}

public func parseMessage(_ data: Data!) -> Any! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public enum SentSecureValueType: Int32 {
case temporaryRegistration = 12
}

public enum BotSendMessageAccessGrantedType: Int32 {
case attachMenu = 0
case request = 1
}

public enum TelegramMediaActionType: PostboxCoding, Equatable {
public enum ForumTopicEditComponent: PostboxCoding, Equatable {
case title(String)
Expand Down Expand Up @@ -86,6 +91,7 @@ public enum TelegramMediaActionType: PostboxCoding, Equatable {
case paymentSent(currency: String, totalAmount: Int64, invoiceSlug: String?, isRecurringInit: Bool, isRecurringUsed: Bool)
case customText(text: String, entities: [MessageTextEntity])
case botDomainAccessGranted(domain: String)
case botAppAccessGranted(appName: String?, type: BotSendMessageAccessGrantedType?)
case botSentSecureValues(types: [SentSecureValueType])
case peerJoined
case phoneNumberRequest
Expand Down Expand Up @@ -195,6 +201,8 @@ public enum TelegramMediaActionType: PostboxCoding, Equatable {
} else {
self = .unknown
}
case 35:
self = .botAppAccessGranted(appName: decoder.decodeOptionalStringForKey("app"), type: decoder.decodeOptionalInt32ForKey("atp").flatMap { BotSendMessageAccessGrantedType(rawValue: $0) })
default:
self = .unknown
}
Expand Down Expand Up @@ -362,6 +370,18 @@ public enum TelegramMediaActionType: PostboxCoding, Equatable {
case let .setSameChatWallpaper(wallpaper):
encoder.encodeInt32(34, forKey: "_rawValue")
encoder.encode(TelegramWallpaperNativeCodable(wallpaper), forKey: "wallpaper")
case let .botAppAccessGranted(appName, type):
encoder.encodeInt32(35, forKey: "_rawValue")
if let appName = appName {
encoder.encodeString(appName, forKey: "app")
} else {
encoder.encodeNil(forKey: "app")
}
if let type = type {
encoder.encodeInt32(type.rawValue, forKey: "atp")
} else {
encoder.encodeNil(forKey: "atp")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,72 @@ func _internal_requestAppWebView(postbox: Postbox, network: Network, stateManage
|> castError(RequestAppWebViewError.self)
|> switchToLatest
}

func _internal_canBotSendMessages(postbox: Postbox, network: Network, botId: PeerId) -> Signal<Bool, NoError> {
return postbox.transaction { transaction -> Signal<Bool, NoError> in
guard let bot = transaction.getPeer(botId), let inputUser = apiInputUser(bot) else {
return .single(false)
}

return network.request(Api.functions.bots.canSendMessage(bot: inputUser))
|> `catch` { _ -> Signal<Api.Bool, NoError> in
return .single(.boolFalse)
}
|> map { result -> Bool in
if case .boolTrue = result {
return true
} else {
return false
}
}
}
|> switchToLatest
}

func _internal_allowBotSendMessages(postbox: Postbox, network: Network, stateManager: AccountStateManager, botId: PeerId) -> Signal<Never, NoError> {
return postbox.transaction { transaction -> Signal<Never, NoError> in
guard let bot = transaction.getPeer(botId), let inputUser = apiInputUser(bot) else {
return .never()
}

return network.request(Api.functions.bots.allowSendMessage(bot: inputUser))
|> map(Optional.init)
|> `catch` { _ -> Signal<Api.Updates?, NoError> in
return .single(nil)
}
|> map { updates -> Api.Updates? in
if let updates = updates {
stateManager.addUpdates(updates)
}
return updates
}
|> ignoreValues
}
|> switchToLatest
}

public enum InvokeBotCustomMethodError {
case generic
}

func _internal_invokeBotCustomMethod(postbox: Postbox, network: Network, botId: PeerId, method: String, params: String) -> Signal<String, InvokeBotCustomMethodError> {
let params = Api.DataJSON.dataJSON(data: params)
return postbox.transaction { transaction -> Signal<String, InvokeBotCustomMethodError> in
guard let bot = transaction.getPeer(botId), let inputUser = apiInputUser(bot) else {
return .fail(.generic)
}
return network.request(Api.functions.bots.invokeWebViewCustomMethod(bot: inputUser, customMethod: method, params: params))
|> mapError { _ -> InvokeBotCustomMethodError in
return .generic
}
|> map { result -> String in
if case let .dataJSON(data) = result {
return data
} else {
return ""
}
}
}
|> castError(InvokeBotCustomMethodError.self)
|> switchToLatest
}
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,18 @@ public extension TelegramEngine {
public func sendWebViewData(botId: PeerId, buttonText: String, data: String) -> Signal<Never, SendWebViewDataError> {
return _internal_sendWebViewData(postbox: self.account.postbox, network: self.account.network, stateManager: self.account.stateManager, botId: botId, buttonText: buttonText, data: data)
}

public func canBotSendMessages(botId: PeerId) -> Signal<Bool, NoError> {
return _internal_canBotSendMessages(postbox: self.account.postbox, network: self.account.network, botId: botId)
}

public func allowBotSendMessages(botId: PeerId) -> Signal<Never, NoError> {
return _internal_allowBotSendMessages(postbox: self.account.postbox, network: self.account.network, stateManager: self.account.stateManager, sbotId: botId)
}

public func invokeBotCustomMethod(botId: PeerId, method: String, params: String) -> Signal<String, InvokeBotCustomMethodError> {
return _internal_invokeBotCustomMethod(postbox: self.account.postbox, network: self.account.network, botId: botId, method: method, params: params)
}

public func addBotToAttachMenu(botId: PeerId, allowWrite: Bool) -> Signal<Bool, AddBotToAttachMenuError> {
return _internal_addBotToAttachMenu(accountPeerId: self.account.peerId, postbox: self.account.postbox, network: self.account.network, botId: botId, allowWrite: allowWrite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,16 @@ public func universalServiceMessageString(presentationData: (PresentationTheme,
attributedString = stringWithAppliedEntities(text, entities: entities, baseColor: primaryTextColor, linkColor: primaryTextColor, baseFont: titleFont, linkFont: titleBoldFont, boldFont: titleBoldFont, italicFont: titleFont, boldItalicFont: titleBoldFont, fixedFont: titleFont, blockQuoteFont: titleFont, underlineLinks: false, message: message._asMessage())
case let .botDomainAccessGranted(domain):
attributedString = NSAttributedString(string: strings.AuthSessions_Message(domain).string, font: titleFont, textColor: primaryTextColor)
case let .botAppAccessGranted(appName, type):
let text: String
if type == .attachMenu {
text = strings.Notification_BotWriteAllowedMenu
} else if type == .request {
text = strings.Notification_BotWriteAllowedRequest
} else {
text = strings.AuthSessions_MessageApp(appName ?? "").string
}
attributedString = NSAttributedString(string: text, font: titleFont, textColor: primaryTextColor)
case let .botSentSecureValues(types):
var typesString = ""
var hasIdentity = false
Expand Down
2 changes: 1 addition & 1 deletion submodules/TelegramUI/Sources/ChatController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13188,7 +13188,7 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G
}

let buttons: Signal<([AttachmentButtonType], [AttachmentButtonType], AttachmentButtonType?), NoError>
if !isScheduledMessages {
if !isScheduledMessages && !peer.isDeleted {
buttons = self.context.engine.messages.attachMenuBots()
|> map { attachMenuBots in
var buttons = availableButtons
Expand Down
Loading

0 comments on commit 882d362

Please sign in to comment.