-
Notifications
You must be signed in to change notification settings - Fork 104
/
configure.js
89 lines (74 loc) · 3.59 KB
/
configure.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
import axios from "axios";
import { createInterface } from "readline";
import fs from "fs";
import { promisify } from "util";
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
const question = (question) =>
new Promise((resolve) => rl.question(question, resolve));
function exitError(error) {
console.error(`Error! ${error}`);
process.exit(1);
}
const banner = `
████████╗██╗ ██╗ █████╗ ████████╗███████╗███╗ ███╗██████╗ ██╗ █████╗ ████████╗███████╗
╚══██╔══╝██║ ██║██╔══██╗ ╚══██╔══╝██╔════╝████╗ ████║██╔══██╗██║ ██╔══██╗╚══██╔══╝██╔════╝
██║ ██║ █╗ ██║███████║ ██║ █████╗ ██╔████╔██║██████╔╝██║ ███████║ ██║ █████╗
██║ ██║███╗██║██╔══██║ ██║ ██╔══╝ ██║╚██╔╝██║██╔═══╝ ██║ ██╔══██║ ██║ ██╔══╝
██║ ╚███╔███╔╝██║ ██║ ██║ ███████╗██║ ╚═╝ ██║██║ ███████╗██║ ██║ ██║ ███████╗
╚═╝ ╚══╝╚══╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚══════╝
`;
console.log(banner);
let githubUsername, githubRepo, botUsername;
(async () => {
try {
const file = fs.readFileSync(".git/config").toString();
const url = file.match(/url = (.*)/)[1];
console.log(url);
const params = url.match(/github.com[/:]([^/]*)\/(.*)\.git/);
githubUsername = params[1];
githubRepo = params[2];
} catch (e) {}
const accessToken = await question("Enter your bot access token: ");
if (!accessToken?.length > 0) exitError("Token is required");
const githubUsernameQ = await question(
`Enter your github username${
githubUsername ? ` (${githubUsername})` : ``
}: `
);
githubUsername = githubUsernameQ || githubUsername;
if (!githubUsername?.length > 0) exitError("Github username is required");
const githubRepoQ = await question(
`Enter your forked repo name${githubRepo ? ` (${githubRepo})` : ``}: `
);
githubRepo = githubRepoQ || githubRepo;
if (!githubRepo?.length > 0) exitError("Repo name is required");
const getBot = await axios.get(
`https://api.telegram.org/bot${accessToken}/getMe`
).catch(exitError);
botUsername = getBot.data.result.username;
const url = `https://${githubUsername}.github.io/${githubRepo}`;
console.log(`\n\nSetting bot ${botUsername} webapp url to ${url}`);
const resp = await axios.post(
`https://api.telegram.org/bot${accessToken}/setChatMenuButton`,
{
menu_button: {
type: "web_app",
text: "Launch Webapp",
web_app: {
url: url,
},
},
}
).catch(exitError);
if (resp.status === 200) {
console.log(
`\nYou're all set! Visit https://t.me/${botUsername} to interact with your bot`
);
process.exit();
} else {
exitError(`\nSomething went wrong! ${resp.error}`);
}
})();