|
| 1 | +--- |
| 2 | +title: Build a Telegram Mini-Game in Unity |
| 3 | +description: This guide covers the creation of a Telegram Game using Sequence's Unity SDK. |
| 4 | +--- |
| 5 | + |
| 6 | +# Build a Telegram Mini-Game in Unity |
| 7 | + |
| 8 | +This guide will walk you through setting up your Telegram Mini-app or game using Sequence's Unity SDK, enabling web3 |
| 9 | +functionality on EVM chains for your users, for email and guest authentication methods. |
| 10 | + |
| 11 | +We’ll develop the game logic in Unity, but to deploy it as a Telegram Mini-Game, we’ll package the WebGL |
| 12 | +build within a React project. |
| 13 | + |
| 14 | +## Setup your Telegram Bot |
| 15 | + |
| 16 | +::::steps |
| 17 | +### Create a Telegram Bot with BotFather |
| 18 | + |
| 19 | +1. Open Telegram and search for "@BotFather" |
| 20 | +2. Start a conversation by clicking "Start" or typing `/start` |
| 21 | +3. Create a new bot by typing `/newbot` |
| 22 | +4. Follow the prompts: |
| 23 | +- Enter a name for your bot |
| 24 | +- Choose a username (must end in 'bot') |
| 25 | + |
| 26 | +### Obtain Required Tokens |
| 27 | + |
| 28 | +After creating your bot, BotFather will provide: |
| 29 | +- Bot Token (HTTP API Token) |
| 30 | +- Bot Username |
| 31 | + |
| 32 | +Save these credentials - you'll need them for the next steps. |
| 33 | +:::: |
| 34 | + |
| 35 | +## Prepare your Unity WebGL Project |
| 36 | + |
| 37 | +::::steps |
| 38 | +### Install Sequence's Unity SDK |
| 39 | + |
| 40 | +Use one of our [recommend installation options](/sdk/unity/installation) for your existing project or use our |
| 41 | +[Unity Boilerplate](https://github.com/0xsequence/unity-embedded-wallet-boilerplate) if you want to start a project from scratch. |
| 42 | + |
| 43 | +### UI for Mobile Layouts |
| 44 | + |
| 45 | +Ensure that your game's UI is optimized for mobile aspect ratios. We recommend using the |
| 46 | +[Device Simulator for Unity](https://docs.unity3d.com/2022.3/Documentation/Manual/device-simulator.html). |
| 47 | +Additionally, ensure your input handling is set up for touch controls. |
| 48 | + |
| 49 | +### Configure Sequence's React Template for WebGL |
| 50 | + |
| 51 | +Sequence's Unity SDK includes a WebGL Template for exporting as a React project. |
| 52 | +Copy the `WebGLTemplates` folder from `Packages/Sequence Embedded Wallet SDK/` into your project's `Assets/` directory. |
| 53 | +Go to `Project Settings` -> `Player` -> `WebGL` -> `Resolution and Presentation` and select the `SequenceReact` template. |
| 54 | + |
| 55 | +### Cloudflare Configuration |
| 56 | + |
| 57 | +Inside the React project's root directory, create a `wrangler.toml` file and include the following content. |
| 58 | + |
| 59 | +```shell |
| 60 | +name = "telegrambot" |
| 61 | +compatibility_date = "2024-03-25" |
| 62 | +pages_build_output_dir = "./dist" |
| 63 | +[vars] |
| 64 | +BOT_TOKEN = "GET_YOUR_BOT_TOKEN_FROM_BOTFATHER" |
| 65 | +BOT_SECRET = "can_be_any_long_random_string_abc123_do_not_share" |
| 66 | + |
| 67 | +[dev] |
| 68 | +port = 4444 |
| 69 | +``` |
| 70 | + |
| 71 | +### Build your WebGL game as a React project |
| 72 | + |
| 73 | +Open the root directory of your React project in a CLI or an IDE like Visual Studio Code, and run the following |
| 74 | +command to test your game in the browser. |
| 75 | + |
| 76 | +``` |
| 77 | +pnpm install |
| 78 | +pnpm run dev |
| 79 | +``` |
| 80 | +:::: |
| 81 | + |
| 82 | +## Deploy your Telegram Mini-Game |
| 83 | + |
| 84 | +::::steps |
| 85 | +### Authenticate with Cloudflare Account |
| 86 | +From the React project's root directory, deploy the app and functions logic to Cloudflare. |
| 87 | + |
| 88 | +1. Run `pnpm wrangler pages deploy` |
| 89 | + |
| 90 | +This will prompt you to authenticate with Cloudflare if not already logged in. On successful deploy, |
| 91 | +copy the URL where your application is now hosted. |
| 92 | + |
| 93 | +### Register Webhook with Telegram |
| 94 | + |
| 95 | +Now we call our `registerWebhook` endpoint on our app to register the webhook function with the URL of your deployed |
| 96 | +application to enable access for the Telegram API. |
| 97 | + |
| 98 | +```shell |
| 99 | +curl -X POST \ |
| 100 | + "<YOUR_CLOUDFLARE_DEPLOYED_URL>/api/registerWebhook" |
| 101 | +``` |
| 102 | + |
| 103 | +### Update Allowed Origins |
| 104 | +Ensure in [Sequence Builder](https://sequence.build/project/default/embedded-wallet) that your wallet configuration includes the URL of your deployed application |
| 105 | +as an allowed origin. |
| 106 | +:::: |
| 107 | + |
| 108 | +### Test out your Telegram Game! |
| 109 | +Send a message to your bot on Telegram and check if it responds! It should answer you with the ability to share, |
| 110 | +play or get more information about the bot. |
0 commit comments