-
Notifications
You must be signed in to change notification settings - Fork 0
/
TgServer.js
125 lines (105 loc) · 4.09 KB
/
TgServer.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
const axios = require('axios');
const {
SQSClient,
ReceiveMessageCommand,
DeleteMessageCommand,
} = require("@aws-sdk/client-sqs");
const dotenv = require('dotenv');
dotenv.config();
const TELEGRAM_TOKEN = process.env.MY_TOKEN;
const TELEGRAM_CHAT_ID = process.env.MY_CHAT_ID;
const sendTelegramMessage = async (text, videoUrl, linkUrl = null) => {
const apiUrl = `https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendVideo`;
// Prepare the payload
const payload = {
chat_id: TELEGRAM_CHAT_ID,
video: videoUrl,
caption: text,
};
// If a link is provided, add an inline keyboard with the button
if (linkUrl) {
payload.reply_markup = {
inline_keyboard: [
[
{
text: "🔗Go To Task", // Button text
url: linkUrl // URL to redirect to when clicked
}
]
]
};
}
try {
await axios.post(apiUrl, payload);
console.log("Video with text and button sent to Telegram successfully!");
} catch (error) {
console.error("Error sending message to Telegram:", error);
}
};
const Task_Created_Queue_url = process.env.MY_TASK_CREATED_SQS;
const Task_Paid_Queue_url = process.env.MY_TASK_PAID_SQS;
const sqsClient = new SQSClient({
endpoint: process.env.AWS_ENDPOINT_SQS,
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});
async function receiveAndProcessSQSMessage(queue_url, type) {
try {
const receiveMessageCommand = new ReceiveMessageCommand({
QueueUrl: queue_url,
MaxNumberOfMessages: 1,
});
const data = await sqsClient.send(receiveMessageCommand);
if (data.Messages && data.Messages.length > 0) {
const message = data.Messages[0];
console.log("Received message:", message.Body);
const response = JSON.parse(message.Body);
let MessageToUser;
let videoUrl;
if(type === 'CreateTask'){
MessageToUser = parseTask(response);
videoUrl = process.env.TASK_CREATED_VIDEO_URL;
await sendTelegramMessage(MessageToUser, videoUrl, process.env.TASK_BASE_URL + response.id);
}else if(type === 'TaskPaid'){
MessageToUser = parseTaskPaid(response);
videoUrl = process.env.TASK_PAID_VIDEO_URL
await sendTelegramMessage(MessageToUser, videoUrl, process.env.TASK_BASE_URL + response.task.id);
}
const deleteMessageCommand = new DeleteMessageCommand({
QueueUrl: queue_url,
ReceiptHandle: message.ReceiptHandle,
});
await sqsClient.send(deleteMessageCommand);
console.log("Message deleted from SQS.");
}
} catch (error) {
console.error("Error receiving or processing message from SQS:", error);
}
}
// Create Message for Task Created
function parseTask(response){
const amount = parseFloat(response.asset.amount);
const decimals = response.asset.decimals || 0;
let amnt = amount / (10 ** decimals);
let roundedAmount = amnt.toFixed(2);
return `🚨 New Task Alert: ${response['title']}! 🚨\n
💰 Reward: ${roundedAmount}${response.asset.symbol} (~$${response.asset.price.toFixed(2)})`;
}
// Create Message for Task Paid (Waiting for Task Paid Payload)
function parseTaskPaid(response){
const amount = parseFloat(response.submission.asset.amount);
const decimals = response.submission.asset.decimals || 0;
let amnt = amount / (10 ** decimals);
let roundedAmount = amnt.toFixed(2);
return `🎉 ${response.submission.user.username} Just Got Paid! 🎉\n \n🎯 Task: ${response.task.title}`
}
function pollMessages() {
setInterval(() => {
receiveAndProcessSQSMessage(Task_Created_Queue_url, 'CreateTask');
receiveAndProcessSQSMessage(Task_Paid_Queue_url, 'TaskPaid');
}, 5000);
}
pollMessages();