-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·81 lines (72 loc) · 2.55 KB
/
Copy pathindex.js
File metadata and controls
executable file
·81 lines (72 loc) · 2.55 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
require('dotenv').config();
const express = require('express');
const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');
const TELEGRAM_API = `https://api.telegram.org/bot${process.env.TOKEN}`;
const URI = `/webhook/${process.env.TOKEN}`;
const WEBHOOK_URL = process.env.SERVER_URL + URI;
//send message
const sendMessage = async () => {
let str = 'Hello World!'
try {
const messageSent = await axios.post(`${TELEGRAM_API}/sendMessage`, {
chat_id: process.env.CHAT_ID,
text: `Bot message:\n${str}`
});
if (messageSent) console.log('message sent!');
} catch (error) {
console.log(error);
}
}
//sendMessage();
//send image
const sendImage = async () => {
let readStream = fs.createReadStream("./files/roma.jpg");
let form = new FormData();
form.append("chat_id", process.env.CHAT_ID);
//form.append("photo", 'https://static.vecteezy.com/packs/media/components/global/search-explore-nav/img/vectors/term-bg-1-666de2d941529c25aa511dc18d727160.jpg');
form.append("photo", readStream);
form.append("caption", 'demo text');
try {
const imageSent = await axios.post(`${TELEGRAM_API}/sendPhoto`, form, { headers: { "Content-Type": "multipart/form-data; boundary=" + form._boundary }});
if (imageSent) console.log('image sent!');
} catch (error) {
console.log(error);
}
}
//sendImage();
//receive and repeat message
const app = express();
app.use(express.json());
const setWebHook = async () => {
const res = await axios.get(`${TELEGRAM_API}/setWebhook?url=${WEBHOOK_URL}`);
console.log('setWebHook:', res.data);
};
const removeWebHook = async () => {
const res = await axios.get(`${TELEGRAM_API}/setWebhook?remove=`);
console.log('removeWebHook:', res.data);
}
app.post(URI, async (req, res) => {
//console.log(req.body);
if(req.body.message){
const chatId = req.body.message.chat.id;
if (req.body.message.text){
try {
const text = req.body.message.text;
await axios.post(`${TELEGRAM_API}/sendMessage`, {
chat_id: chatId,
text: text
});
} catch (error) {
console.log(error);
}
}
}
return res.send();
});
app.listen(process.env.PORT, async () => {
console.log(`app running on port ${process.env.PORT}`);
//await setWebHook();
//await removeWebHook();
});