-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (66 loc) · 2.07 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
import 'dotenv/config'
import { Telegraf } from 'telegraf'
import {addTextOnImage} from './imageManipulator.js'
const bot = new Telegraf(process.env.BOT_TOKEN)
bot.command('start', (ctx) => {
try {
ctx.reply('Forward or send me a quote')
} catch (error) {
console.log("???????")
}
})
const backgroundImgPath = "assets/bg.png"
bot.on('text', async (ctx) => {
console.log({ctx : ctx.update.message})
let username = ctx.message.from.username
let text = ctx.message.text
if(username)
username = `@${username}`
else
username = ctx.message.from.first_name
let images = await ctx.tg.getUserProfilePhotos(ctx.message.from.id)
let firstProfP = images.photos[0]
const forward_from = ctx.update.message.forward_from
const forward_sender_name = ctx.update.message.forward_sender_name
// forward_sender_name
if(forward_from){
let un = forward_from.username ? `@${forward_from.username}` : null
un = un ? un : `${forward_from.first_name} ${forward_from.last_name ? forward_from.last_name : ''}`
username = un
console.log({username})
images = await ctx.tg.getUserProfilePhotos(forward_from.id)
firstProfP = images.photos[0]
}else if(forward_sender_name){
username = forward_sender_name
firstProfP= null
}
let theQuotePic = null
if(firstProfP?.length){
const profPicImage = await ctx.telegram.getFileLink(firstProfP[2].file_id)
theQuotePic = await addTextOnImage(text, backgroundImgPath, profPicImage.href, username)
}else{
theQuotePic = await addTextOnImage(text, backgroundImgPath, null, username)
}
try {
ctx.reply(`Generating your quote ...`)
ctx.replyWithPhoto({
source: theQuotePic
})
} catch (error) {
console.error({error})
}
})
if (process.env.DEVELOPMENT){
bot.launch()
}else{
bot.launch({
webhook: {
domain: process.env.WEBHOOK_URL,
port: process.env.WEBHOOK_PORT,
host: '0.0.0.0'
}
})
}
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))