skip to Main Content

I don’t know how to connect telegram to angular. In the HTML file I do this –

<script src="./assets/telegram-web-app.js"></script>

But I don’t know what to do in the component to call the function I need.

https://core.telegram.org/bots/webapps#initializing-mini-apps

I searched for a solution on this site but couldn’t find it.

2

Answers


  1. First, add it to your angular.json file:

    "scripts": [
        "./assets/telegram-web-app.js"
    ]
    

    Then in your component, declare a var with the same name as a telegram JS export. This tells angular what to look for in the JS (read more):

    declare var telegramJSObjName:any;
    

    And use it in your component like:

    declare var telegramJSObjName:any;
    
    constructor(){}
    
    callFunction1() {
        telegramJSObjName.func1();
    }
    

    If you have issues, try

    1. adding "allowJs": true within "compilerOptions" in tsconfig.json
    2. creating the file src/typings.d.ts and moving all ‘declare’ statements in here.

    See these posts for more on how to access functions from a javascript file:
    Including JS in Angular (recommended approach)
    Including with a <script> tag

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search