Clipboard Copying Issue in Telegram Mini App on Construct 3

0 favourites
  • 5 posts
From the Asset Store
Casino? money? who knows? but the target is the same!
  • Hello!

    I am creating an HTML5 game using Construct 3 and want to implement a function to copy a referral code to the clipboard when the "Invite" button is clicked in a Telegram Mini App. Here is the code I am using:

    javascript

    const refCode = "YOUR_REFERRAL_CODE";
    
    function copyToClipboard(text) {
     const tempInput = document.createElement("textarea");
     tempInput.value = text;
     document.body.appendChild(tempInput);
     tempInput.select();
     document.execCommand("copy");
     document.body.removeChild(tempInput);
    }
    
    if (Telegram.WebApp) {
     copyToClipboard(refCode);
     Telegram.WebApp.showAlert("Referral code copied to clipboard!");
    } else {
     alert("Referral code copied to clipboard!");
    }
    

    This code works in the browser (Google Chrome), but it does not work in the Telegram Mini App window. When the button is clicked, nothing happens. How can I correctly implement text copying to the clipboard within the Telegram Mini App? Thank you in advance!

  • That particular way to access the clipboard is depreciated in most browsers. There’s a newer way you could try. Just google the clipboard JavaScript api. But in general clipboard access doesn’t seem well supported across browsers. No idea what the telegram mini app supports, maybe it has a different way to access the clipboard.

    Looking at the error console after running that code could possibly give the error.

  • unfortunately this did not help, perhaps someone from the construct team can answer this question? The thing is that I tried all sorts of methods, and I can neither copy at the click of a button while in the game inside the telegram web app, nor call the modal window "share" to share the link to the game among the chat telegram. I'm confused.

  • I mean that has nothing to do with construct. But you can wait on their reply if they even do reply.

    Here is the current standard way of writing to the clipboard from the browser if you want something to mess with now.

    developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText

    If it doesn’t work then you need to somehow look at the errors and exemptions that are usually shown on the browser console. If you can’t access the browser console then you need to display it another way. Worst case clipboard access isn’t supported in the browser engine that app you’re using. Only workaround then is to make an editbox with the text and let the user select and copy it. Or you could do your own fake clipboard if you’re only copying/pasting within your game.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • If Telegram Mini Apps use a WebView, then it should work. If it's a custom engine then it will probably have loads of compatibility problems like this. Either way, your question would be best directed to Telegram Mini App support, as they make the app platform in question.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)