-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusers.js
More file actions
141 lines (124 loc) · 4.13 KB
/
Copy pathusers.js
File metadata and controls
141 lines (124 loc) · 4.13 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const cloudinary = require('cloudinary').v2;
const Airtable = require('airtable');
// Configure Cloudinary
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME, // Replace with your Cloudinary cloud name
api_key: process.env.CLOUDINARY_API_KEY, // Replace with your API key
api_secret: process.env.CLOUDINARY_API_SECRET, // Replace with your API secret
});
// Configure Airtable
const airtableBase = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY_TOKEN })
.base(process.env.AIRTABLE_BASE_ID);
/**
* Airtable Users table fields:
* {
* id: <Integer>, // Telegram user ID
* first_name: <String>, // First name
* last_name: <String>, // Last name
* nick_name: <String>, // Username
* phone: <String>, // Phone number
* cloudinary_avatar: <String> // URL to the avatar image in Cloudinary
* created_at: <Date>, // Creation date
* updated_at: <Date> // Last update date
* }
*/
/**
* @param buffer - File buffer.
* @returns Promise with the Cloudinary image object in result.
*/
const uploadImageBuffer = async (buffer) => {
return new Promise((resolve, reject) => {
const stream = cloudinary.uploader.upload_stream(
{ folder: 'telegram-avatars' }, // Optional: specify folder or other options
(error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
}
);
// Write the buffer to the stream
stream.end(buffer);
});
};
/**
* Search for a user by Telegram message ID in Airtable database.
* If not found - insert it.
* If no avatar set - look for the avatar image in Telegram and update Airtable user if exists.
*
* @param message - Telegram message object:
* {
* fromId: {...},
* ...
* }
* @returns The sender object with retrieved Telegram entity values + Airtable fields
* or (if no access) only { id: <Integer> } object
*/
async function getMessageUser(client, message) {
try {
// trying to find a user using Telegram API (if not in contacts or dialogues will throw an exception)
const fromEntity = await client.getEntity(message.fromId);
// Airtable user
let airtableUser = null;
// finding a user in Airtable
const airtableUsers = await airtableBase('Users')
.select({
filterByFormula: `AND({id} = "${fromEntity.id}")`,
maxRecords: 1,
})
.firstPage();
if (airtableUsers && airtableUsers.length) {
airtableUser = airtableUsers[0];
}
// creating a user in Airtable
if (!airtableUser) {
const newUser = {
id: fromEntity.id.toString(),
first_name: fromEntity.firstName,
last_name: fromEntity.lastName,
};
if (fromEntity.username) {
newUser.nick_name = fromEntity.username;
}
if (fromEntity.phone) {
newUser.phone = fromEntity.phone;
}
airtableUser = await airtableBase('Users')
.create(newUser);
} else {
// updating existing Airtable user
airtableUser = await airtableBase('Users')
.update(airtableUser.id, {
first_name: fromEntity.firstName,
last_name: fromEntity.lastName,
nick_name: fromEntity.username,
phone: fromEntity.phone,
});
}
// updating avatar if not set yet
// TODO: find a way to update photos in Airtable when they become updated by users in Telegram
if (!airtableUser.fields.cloudinary_avatar && fromEntity.photo && fromEntity.photo.strippedThumb) {
const buffer = await client.downloadProfilePhoto(message.fromId.userId)
const result = await uploadImageBuffer(buffer);
airtableUser = await airtableBase('Users')
.update(airtableUser.id, {
cloudinary_avatar: result.secure_url,
});
}
fromEntity.airtableUser = airtableUser.fields;
return {
...(({ id, firstName, lastName, username, phone }) => ({ id, firstName, lastName, username, phone }))(fromEntity),
airtable: airtableUser.fields
};
} catch (error) {
console.log('Error getting the fromId entity');
console.log(error.message);
return {
id: message.fromId.userId,
};
}
}
module.exports = {
getMessageUser,
}