forked from vrumger/ChannelHashBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateComment.js
More file actions
83 lines (72 loc) · 2.25 KB
/
Copy pathcreateComment.js
File metadata and controls
83 lines (72 loc) · 2.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
const request = require(`request-promise`);
const uploadPhoto = async photo => {
const uploadedPhoto = await request.post({
method: `POST`,
uri: `https://telegra.ph/upload`,
json: true,
formData: {
file: {
value: Buffer.from(photo, `base64`),
options: {
filename: `image.jpg`,
contentType: `image/jpeg`,
},
},
},
});
return `https://telegra.ph${uploadedPhoto[0].src}`;
};
module.exports = (ctx, next) => {
ctx.downloadPhoto = async function() {
const message = this.message || this.editedMessage;
const photos = [...message.photo];
const photo = photos.pop().file_id;
const link = await ctx.telegram.getFileLink(photo);
return await request({
uri: link,
encoding: null,
});
};
ctx.createComment = async (text, options) => {
let comment;
const commentOptions = {
api_key: process.env.COMMENTS_API_KEY,
owner_id: 234480941,
type: `text`,
caption: text,
text: text,
parse_mode: `html`,
};
try {
if (ctx.photo) {
const photo = await ctx.downloadPhoto();
const telegraphUrl = await uploadPhoto(photo);
commentOptions.type = `photo`;
commentOptions.photo_url = telegraphUrl;
}
comment = await request({
url: `https://api.comments.bot/createPost`,
json: true,
body: commentOptions,
});
} catch (_) {
// Ignore errors
}
if (comment) {
if (!options.reply_markup) {
options.reply_markup = { inline_keyboard: [] };
}
options.reply_markup.inline_keyboard.push([
{
text: `View Comments`,
login_url: {
url: comment.result.link,
forward_text: `View Comments`,
bot_username: `CommentsBot`,
},
},
]);
}
};
next(ctx);
};