forked from vrumger/ChannelHashBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateComment.ts
More file actions
97 lines (85 loc) · 2.85 KB
/
Copy pathcreateComment.ts
File metadata and controls
97 lines (85 loc) · 2.85 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
import { TContext, TNext } from '../typings';
import { InlineKeyboardMarkup } from 'telegraf/typings/telegram-types';
import request from 'request-promise';
const uploadPhoto = async (photo: Buffer) => {
const uploadedPhoto = await request.post({
method: 'POST',
uri: 'https://telegra.ph/upload',
json: true,
formData: {
file: {
value: photo,
options: {
filename: 'image.jpg',
contentType: 'image/jpeg',
},
},
},
});
return `https://telegra.ph${uploadedPhoto[0].src}`;
};
export default (ctx: TContext, next: TNext): void => {
ctx.downloadPhoto = async function () {
const message = (this.message || this.editedMessage)!;
const photos = [...message.photo!];
const photo = photos.pop()!.file_id;
const link = await this.telegram.getFileLink(photo);
return await request({
uri: link,
encoding: null,
});
};
ctx.createComment = async function (
text: string,
admins: number[],
options,
) {
const message = (this.message || this.editedMessage)!;
let comment;
const commentOptions: { [k: string]: string | number } = {
api_key: process.env.COMMENTS_API_KEY as string,
owner_id: 234480941,
type: 'text',
caption: text,
text: text,
parse_mode: 'html',
administrators: admins.join(','),
};
try {
if (message.photo) {
const photo = await this.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 error
}
if (comment) {
if (!options.reply_markup) {
options.reply_markup = { inline_keyboard: [] };
}
// TODO:
(options.reply_markup as InlineKeyboardMarkup).inline_keyboard.push(
[
{
text: 'View Comments',
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore telegram-typings is outdated
login_url: {
url: comment.result.link,
forward_text: 'View Comments',
bot_username: 'CommentsBot',
},
},
],
);
}
};
next();
};