-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtelegram-subscription.ts
More file actions
107 lines (93 loc) · 3.25 KB
/
Copy pathtelegram-subscription.ts
File metadata and controls
107 lines (93 loc) · 3.25 KB
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
/**
* telegram-subscription service
*/
import { factories } from '@strapi/strapi'
import { env } from '@strapi/utils'
import fetch from 'node-fetch'
import crypto from 'crypto'
import { TelegramData } from '../types'
import { templateNotification } from '../../notification/services/notification'
const MODULE_ID = 'api::telegram-subscription.telegram-subscription'
const SEND_MESSAGE_URL = `https://api.telegram.org/bot${env('TELEGRAM_SECRET')}/sendMessage`
export default factories.createCoreService(MODULE_ID, ({strapi}) => {
return {
async verifyTgAuthentication(data: TelegramData) {
const dataString = Object.keys(data).reduce((acc, key) => {
if (key === 'hash') return acc
acc.push(`${key}=${data[key]}`)
return acc
}, []).sort().join('\n')
const secretHash = crypto.createHash('sha256').update(env('TELEGRAM_SECRET')).digest('base64')
const result = crypto.createHmac('sha256', new Buffer(secretHash, 'base64')).update(dataString).digest('hex')
return result === data.hash
},
async addSubscription(account: string, data: TelegramData) {
return strapi.entityService.create(
MODULE_ID,
{
data: {
account,
authDate: data.auth_date,
firstName: data.first_name,
hash: data.hash,
chatId: data.id,
photoUrl: data.photo_url,
username: data.username,
}
})
},
async removeSubscriptions(account: string) {
const subscriptions = await this.getAccountSubscriptions(account)
for (const subscription of subscriptions) {
await strapi.entityService.delete(MODULE_ID, subscription.id)
}
return true
},
async getSubscriptions(accounts: string[]): Promise<{id: string, account: string, chatId: string}[]> {
return strapi.entityService.findMany(
MODULE_ID,
{
filters: {
account: {
$in: accounts
}
},
fields: ['id', 'account', 'chatId']
}
)
},
async getAccountSubscriptions(account: string): Promise<{id: string, account: string, chatId: string}[]> {
return strapi.entityService.findMany(
MODULE_ID,
{
filters: {
account
},
fields: ['id', 'account', 'chatId']
}
)
},
// TODO: temporary implementation
async sendNotifications(): Promise<number> {
const notifications = await strapi.service('api::notification.notification').getPushNotifications()
if (notifications.length === 0) return 0
const subscriptions = await this.getSubscriptions(notifications.map(n => n.account))
const requests = subscriptions.map(subscription => {
return notifications.map(notification => {
return fetch(SEND_MESSAGE_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chat_id: subscription.chatId,
text: templateNotification(notification.notification_template.description, notification.data)
})
})
})
}).flat()
await Promise.all(requests)
return requests.length
}
}
});