atipugin / telegram-bot-ruby

Ruby wrapper for Telegram's Bot API
https://core.telegram.org/bots/api
Do What The F*ck You Want To Public License
1.34k stars 216 forks source link

Telegram new launch: Web_app #253

Closed antondgx closed 1 year ago

antondgx commented 2 years ago

Is telegram's web_app feature supported by telegram-bot-ruby already? The "web_app" field in inline buttons doesn't seem to work with telegram-bot-ruby (https://core.telegram.org/bots/api#inlinekeyboardbutton)

atipugin commented 2 years ago

Hi @antondgx,

Yes, web apps are supported

antondgx commented 2 years ago

That is very cool and quick update! Thank you for building and maintaining this gem!

Im trying to figure out how to open the web app via inline button. below is a sample code. can you let me know what is wrong? "../web_app.html.erb" is the html file that i want to display. It gives this error: "uninitialized constant Telegram::Bot::Types::WebAppInfo (NameError)"

Sample code: def test(user, message_obj) final_obj = check_message_type(message_obj) web_app_info = Telegram::Bot::Types::WebAppInfo.new(url: "../web_app.html.erb")

Telegram::Bot::Client.run(@token) do |bot|
  bot.api.editMessageText(chat_id: final_obj["chat"]["id"], message_id: final_obj["message_id"], text: "Goodbye #{user.first_name}! Press /start to start again", web_app: web_app_info)
end

end

antondgx commented 2 years ago

below line gives this error: "uninitialized constant Telegram::Bot::Types::WebAppInfo (NameError)". I cant figure out whats wrong. "http://localhost:3000/web_app.html" is a html in my rails application. Can anyone help?

web_app_info = Telegram::Bot::Types::WebAppInfo.new(url: "http://localhost:3000/web_app.html")

atipugin commented 2 years ago

Have you updated gem up to the recent version?

antondgx commented 2 years ago

Hey, Thanks! Indeed the gem is not updated. After i installed the latest version there is no more error. However there is no reaction from my bot when I press inline button on my bot to display the intended html page. Any idea what is the problem? how do i connect the backend with the html page? Below are my sample codes:

backend code (a function in my bot logic):

  def bye(user, message_obj)
    final_obj = check_message_type(message_obj)
    Telegram::Bot::Client.run(@token) do |bot|
      web_app_info = Telegram::Bot::Types::WebAppInfo.new(url: "https://604d-27-125-173-138.ap.ngrok.io/web_app")
      kb = [Telegram::Bot::Types::InlineKeyboardButton.new(text: "WEB APP!!", web_app: web_app_info)]
      markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: kb)
      bot.api.send_message(chat_id: final_obj["chat"]["id"], message_id: final_obj["message_id"], text: "Open Web App!!", reply_markup: markup)
    end
  end

HTML code (web_app.html):

<html>
  <head>
    <meta charset="utf-8">
    <title>Test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no, viewport-fit=cover" />
    <meta name="format-detection" content="telephone=no" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="MobileOptimized" content="176" />
    <meta name="HandheldFriendly" content="True" />
    <meta name="robots" content="noindex, nofollow" />
    <script src="https://telegram.org/js/telegram-web-app.js"></script>
    <script>
      function setThemeClass() {
        document.documentElement.className = Telegram.WebApp.colorScheme;
      }
      Telegram.WebView.onEvent('theme_changed', setThemeClass);
      setThemeClass();
    </script>
  </head>

  <body>
    <h1> HELLO WORLD </h1>
  </body>
</html>
antondgx commented 2 years ago

Can Anyone help? Thanks

atipugin commented 2 years ago

@antondgx i haven't tried web apps yet, gonna give it a try this weekend

antondgx commented 2 years ago

Awesome! I look forward for some guidance for this. This will be such a game changer for telegram bots in future!

atipugin commented 2 years ago

Looks like it's an issue with Telegram Desktop app. Following code works just fine in web (https://web.telegram.org) and mobile app, but indeed doesn't work on desktop.

def handle_message(api, message)
  web_app = Telegram::Bot::Types::WebAppInfo.new(url: 'https://google.com')
  kb = [[Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Open Web App', web_app: web_app)]]
  markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: kb)
  api.send_message(chat_id: message.chat.id, text: 'Hey!', reply_markup: markup)
end

Telegram::Bot::Client.run(token) do |bot|
  bot.listen do |message|
    case message
    when Telegram::Bot::Types::Message
      handle_message(bot.api, message)
    end
  end
end

Btw regular KeyboardButton works in desktop app too.