forked from tamimattafi/telegram-mini-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.js
63 lines (52 loc) · 1.86 KB
/
Application.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { launchBot } from "../telegram/Bot.js";
import {launchApi, MESSAGE_PATH} from "../http/Api.js";
/**
* This is the entry point of our app
* Call this method inside index.js to launch the bot and the api
*
*/
export function launchApp() {
// Read token from .env file and use it to launch telegram bot
const bot = launchBot(process.env.BOT_TOKEN)
// Launch api
const api = launchApi()
// Listen to post requests on messages endpoint
api.post(MESSAGE_PATH, async (request, response) => {
await handleMessageRequest(bot, request, response)
})
}
/**
* Receives data from the mini app and sends a simple message using answerWebAppQuery
* @see https://core.telegram.org/bots/api#answerwebappquery
*
* We will use InlineQueryResult to create our message
* @see https://core.telegram.org/bots/api#inlinequeryresult
*/
const handleMessageRequest = async (bot, request, response) => {
try {
// Read data from the request body received by the mini app
const {queryId, message} = request.body
// We are creating InlineQueryResultArticle
// See https://core.telegram.org/bots/api#inlinequeryresultarticle
const article = {
type: 'article',
id: queryId,
title: 'Message from the mini app',
input_message_content: {
message_text: `MiniApp: ${message}`
}
}
// Use queryId and data to send a message to the bot chat
await bot.answerWebAppQuery(queryId, article)
// End the request with a success code
await response.status(200).json({
message: 'success!'
})
} catch (e) {
const errorJson = JSON.stringify(e)
console.log(`handleMessageRequest error ${errorJson}`)
await response.status(500).json({
error: errorJson
})
}
}