-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
121 lines (112 loc) · 4.92 KB
/
index.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
const validUrl = require('valid-url');
const { Telegraf } = require('telegraf');
const short = require('./modules/short');
const unshort = require('./modules/unshort');
const default_btn = [
{ text: "Join Channel", url: "https://t.me/asprojects" },
{ text: "Support Group", url: "https://t.me/assupportchat" },
];
const bot = new Telegraf(process.env.BOT_TOKEN);
const options = (ctx, btnText, btnUrl) => {
return { reply_to_message_id: ctx.update.message.message_id ,
allow_sending_without_reply : true,
disable_web_page_preview : true,
reply_markup: {
inline_keyboard: [[{ text: btnText, url: btnUrl }]]
}
}
}
bot.start((ctx) => {
if(ctx.message.chat.type == 'private'){
ctx.replyWithMarkdown(`Hey ${ctx.message.from.first_name}, Welcome ! \nUse /help to get started. Send me a long URL and get it shortened. \n\nMade with ❤ by [𝔄𝔉𝔉𝔄𝔑](https://t.me/AffanTheBest).` ,
{
reply_to_message_id: ctx.update.message.message_id,
allow_sending_without_reply: true,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [default_btn],
},
}
);
}
});
bot.help((ctx) => {
if(ctx.message.chat.type == 'private'){
ctx.replyWithMarkdown("To short a big URL just send me the long URL and I'll give shorten link.\n\n/start - Restart the bot. \n/help - Get this message. \n/short - Short Long Urls (Eg `/short https://github.com/AffanTheBest`)\n/unshort - Extract long URL from any shortend URL. (Eg. `/unshort https://cutt.ly/qzXU9A2`). \nType `@AsUrlBot` in chat input to use inline. (Eg. `@AsUrlBot Macbook`)\n\n/donate - Donate to developer." ,
{
reply_to_message_id: ctx.update.message.message_id,
allow_sending_without_reply: true,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [default_btn],
},
}
);
}
})
bot.command('donate', async(ctx) => {
const donate_btns = [
{text: 'Ko-fi', url: 'https://ko-fi.com/affanthebest'},
{text: 'Paypal', url: 'https://paypal.me/affanthebest'}
]
ctx.replyWithMarkdown('Thanks for showing intrest in donating. Remember every donation matters!\n\nYou can donate me by using following links:\nPaypal: https://paypal.me/affanthebest\nKo-fi - https://ko-fi.com/affanthebest \n\nUPI - `siddiquiaffan201@okaxis`\n\nFor any other methods contact @AffanTheBest personally.',
{
reply_to_message_id: ctx.update.message.message_id,
allow_sending_without_reply: true,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [donate_btns],
},
});
})
bot.command('unshort' ,async (ctx) => {
const url = ctx.message.text.split(' ').slice(1)[0];
if(validUrl.isUri(url)){
const longUrl = await unshort(url);
ctx.replyWithMarkdown(
`Here's the extracted link : \n👉 ${longUrl} .`, options(ctx, 'Extracted URL', longUrl)
)
}else{
ctx.replyWithMarkdown('Please send a valid URL !')
}
})
bot.command('short' , async(ctx) => {
const url = ctx.message.text.split(' ').slice(1)[0];
if(validUrl.isUri(url)){
const shortReq = await short(url);
const status = shortReq.status;
ctx.replyWithMarkdown(shortReq.msg, options(ctx, status==true ? shortReq.url : '', status==true ? shortReq.url : ''));
}else{
ctx.reply('Please send a valid URL !');
}
})
bot.on('text', async(ctx) => {
if(ctx.message.chat.type == 'private' && validUrl.isUri(ctx.message.text)){
const shortReq = await short(ctx.message.text);
const status = shortReq.status;
ctx.replyWithMarkdown(shortReq.msg, options(ctx, status==true ? shortReq.url : '', status==true ? shortReq.url : ''));
}else if(ctx.message.chat.type == 'private'){
ctx.reply('Please send a valid URL !');
}})
bot.on('inline_query', async(ctx) => {
const method = ctx.inlineQuery.query.split(' ')[0];
const url = ctx.inlineQuery.query.split(' ')[1];
const genArticle = (title, description, message_text) => ({
type: 'article', id: 1, title, description, thumb_url: '',input_message_content:{
message_text, disable_web_page_preview: true, parse_mode: 'markdown'
}
})
if(validUrl.isUri(url)){
if(method == 'short' ){
const shortReq = await short(url);
return await ctx.answerInlineQuery([genArticle('SHORT', `Short ${url}`, shortReq.msg)])
}else if(method == 'unshort' ){
const longUrl = await unshort(url);
return await ctx.answerInlineQuery([genArticle('UNSHORT', `Unshort ${url}`, `Here's the extracted link : \n👉 ${longUrl} .`)])
}
}
})
bot.launch()
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))