This repository has been archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
182 lines (160 loc) · 6.62 KB
/
server.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// init project
const Telegraf = require("telegraf");
const express = require("express");
const axios = require("axios");
const fs = require("fs");
const data = require("./data");
const session = require("telegraf/session");
const Stage = require("telegraf/stage");
const Scene = require("telegraf/scenes/base");
const { leave } = Stage;
const stage = new Stage();
const { Extra, Markup } = Telegraf;
// Get project slug for Glitch and Heroku deployments, fallbacks to default if none
const GLITCH_PROJECT_SLUG = process.PROJECT_DOMAIN || "handsome-sheet";
const HEROKU_APP_NAME = process.env.HEROKU_APP_NAME;
// Pull token
const BOT_TOKEN = process.env.TGBOT_TOKEN;
if (BOT_TOKEN.length > 45) {
console.error("That's too much! Bot tokens must be 45 characters.")
process.exit(1)
} else if (BOT_TOKEN.length < 45) {
console.error("")
process.exit(1)
} else {
console.log("The bot token is successfully retrived. Looking for app url...")
}
// Automatically resolves webhook URls for different deployments.
const webhookReceiverUrl =
"https://" +
GLITCH_PROJECT_SLUG +
".glitch.me/telegram/endpoints/"+BOT_TOKEN
process.env.APP_URL + "/telegram/endpoints/"+BOT_TOKEN
process.env.NOW_URL + "/telegram/endpoints/"+BOT_TOKEN
"https://" +
HEROKU_APP_NAME +
".herokuapp.com/telegram/endpoints/"+BOT_TOKEN
// Get the app base url for diffrent works.
const AppBaseUrl = process.env.APP_URL || "https://"+GLITCH_PROJECT_SLUG+".glitch.me" || process.env.NOW_URL || "https://"+HEROKU_APP_NAME+"herokuapp.com"
// Pull the token to get started.
const bot = new Telegraf(BOT_TOKEN);
bot.use(session());
bot.use(stage.middleware());
// Manage scenes
const getBotInfo = new Scene();
getBotInfo.command("about", ctx => ctx.reply("*About the bot*"));
const speedTest = new Scene()
speedTest.command("status", )
// Get bot information and print from console logs
bot.telegram.getMe().then(bot_informations => {
bot.options.username = bot_informations.username;
console.log(
"The webhook endpoint is ready to deploy. Your Telegram bot username is " +
bot_informations.username
);
});
bot.on('inline_query', ctx => {
let query = ctx.update.inline_query.query; // If you analyze the context structure, query field contains our query.
if(query.startsWith("/")){ // If user input is @yourbot /command
if(query.startsWith("/getAudio Example.ogg")){
// In this case we answer with a list of ogg voice data.
// It will be shown as a tooltip. You can add more than 1 element in this JSON array. Check API usage "InlineResultVoice".
return ctx.answerInlineQuery([
{
type: 'voice', // It's a voice file.
id: ctx.update.inline_query.id, // We reflect the same ID of the request back.
title: 'Send audio file sample.ogg', // Message appearing in tooltip.
voice_url: 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg',
voice_duration: 16, // We can specify optionally the length in seconds.
caption: '[BOT] Audio file sample.ogg!' // What appears after you send voice file.
}
]);
}
}else{ // If user input is @yourbot name
let name_target = query; // Let's assume the query is actually the name.
let message_length = name_target.length; // Name length. We want to ensure it's > 0.
if(message_length > 0){
let full_message;
let dice=Math.floor(Math.random()*8)+1; // Let's throw a dice for a random message. (1, 8)
switch(dice){
case 1: full_message = "Something went berserk went looking for"+name_target; break;
case 2: full_message = "IMHO, "+name_target+" is awesome"; break;
case 3: full_message = name_target+" is not a nice people for me..."; break;
case 4: full_message = name_target+" for me you are c- Eh! You wanted!"; break;
case 5: full_message = "Whoa! "+name_target+" is very cool!"; break;
case 6: full_message = "Grifondoro! No wait, "+name_target+" you're such a noob."; break;
case 7: full_message = "Sometimes I ask myself why people like "+name_target+" dress up and walk around like that..."; break;
case 8: full_message = "Watch him! Watch! "+name_target+" is so ugly!"; break;
}
// Let's return a single tooltip, not cached (In order to always change random value).
return ctx.answerInlineQuery([{
type: 'article',
id: ctx.update.inline_query.id,
title: "Our bot doesn't found that.",
description: 'The Recap Time bot thoght',
input_message_content: {message_text: full_message}
}], {cache_time: 0});
}
}
})
bot.use((ctx, next) => {
const start = new Date()
return next().then(() => {
const ms = new Date() - start
console.log('response time %sms', ms)
})
})
const app = express();
app.get("/", (req, res) =>
res.send({
status: 200,
description: "Hello world! The service is currently running."
})
);
app.use(bot.webhookCallback("/telegram/endpoints/"+BOT_TOKEN))
app.get("/thank-you", (req, res) =>
res.sendFile(__dirname + "/views/thankyou.html")
);
app.get("/report-a-bug", (req, res) =>
res.sendFile(__dirname + "/views/report.html")
);
app.get("/telegram/endpoints/${BOT_TOKEN/metadata}", (req, res) =>
res.send({ botUsername: bot_informations.username }))
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send({
status: 500,
desciption:
"Something went berserk. Either check the code, consult the docs or contact Support",
"issueTracker": "https://gitlab.com/MadeByThePinsTeam-DevLabs/"
});
});
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(404).send({
status: 404,
desciption:
"Whoops! That didn't found on our side. Check the url or change some code.",
triggeredErrorUrl: AppBaseUrl
});
});
var listener = app.listen(process.env.PORT, function() {
console.log(
"Your Express app is listening on port " + listener.address().port
);
});
// Remove the code below for local deployments and deployments outside Glitch.com
// It's better to get your own copy of this project.
process.on("SIGTERM", function() {
console.log(
"SIGTERM received, sending SOS to Resurrect... [Issued on: " +
Date.now() +
"]"
);
require("https").get(
"https://resurrect.glitch.me/" +
GLITCH_PROJECT_SLUG +
"/optional/path/here",
process.exit
);
});