1- use crate :: data:: { add_user, current_timestamp, get_user, has_user, User } ;
1+ use crate :: data:: { add_user, current_timestamp, get_data , get_user, has_user, set_data , User , UserColumn } ;
22use crate :: fetcher:: { parse_tiktok_content, ContentType , Media } ;
33use crate :: parser:: is_tiktok_url;
44use reqwest:: Url ;
55use std:: cmp:: PartialEq ;
66use teloxide:: dispatching:: dialogue:: GetChatId ;
7- use teloxide:: payloads:: { SendMessageSetters , SendPhotoSetters , SendVideoSetters } ;
7+ use teloxide:: payloads:: { SendMessageSetters , SendVideoSetters } ;
88use teloxide:: prelude:: { Message , Requester } ;
99use teloxide:: types:: { ChatId , InputFile , InputMedia , InputMediaPhoto , ParseMode } ;
1010use teloxide:: Bot ;
1111
12+ #[ allow( dead_code) ]
13+ const TEXT_LIMIT : u16 = 4096 ;
14+ const MEDIA_LIMIT : u16 = 1024 ;
15+
1216pub async fn start ( bot : Bot ) {
1317 teloxide:: repl ( bot, |bot : Bot , msg : Message | async move {
1418
@@ -34,6 +38,8 @@ pub async fn start(bot: Bot) {
3438 None => & "en" . to_string ( )
3539 } ;
3640
41+ update_user ( & bot, user. id . 0 , chat_id, & user. first_name , user_lang) . await ;
42+
3743 if !is_tiktok_url ( & text) {
3844 send_msg (
3945 & bot,
@@ -52,44 +58,6 @@ pub async fn start(bot: Bot) {
5258 return Ok ( ( ) ) ;
5359 }
5460
55- if has_user ( user. id . 0 ) . await . unwrap ( ) {
56- let data_user: User = get_user ( user. id . 0 ) . await . unwrap ( ) . unwrap ( ) ;
57-
58- // days after last bot use
59- let time_after_last_use = ( current_timestamp ( ) - data_user. timestamp ) /1000 /60 /60 /24 ;
60-
61- if time_after_last_use >= 3 {
62- send_msg (
63- & bot,
64- chat_id,
65- "<b>🌟 Welcome back!</b>\n 🌟 We missed you! 😊" ,
66- "<b>🌟 С возвращением!</b>\n 🌟 Мы рады снова видеть вас! 😊" ,
67- user_lang
68- ) . await ;
69- }
70-
71-
72- } else {
73- let data_user = User {
74- id : user. id . 0 ,
75- name : user. first_name . clone ( ) ,
76- requests_amount : 1 ,
77- timestamp : current_timestamp ( ) ,
78- register_timestamp : current_timestamp ( ) ,
79- } ;
80- add_user ( data_user) . await . unwrap ( ) ;
81-
82- // wellcome message
83- send_msg (
84- & bot,
85- chat_id,
86- "<b>🎉 Welcome aboard!</b>\n 🎉 It looks like you're new here. Enjoy our bot for free - no limits! 😊" ,
87- "<b>🎉 Добро пожаловать!</b>\n 🎉 Похоже, вы здесь впервые. Пользуйтесь ботом бесплатно - без ограничений! 😊" ,
88- user_lang
89- ) . await ;
90-
91- }
92-
9361 send_msg (
9462 & bot,
9563 chat_id,
@@ -114,14 +82,16 @@ pub async fn start(bot: Bot) {
11482 } ,
11583 } ;
11684
85+ let media_title = cut_text ( media. get_title ( ) , MEDIA_LIMIT ) ;
86+
11787 if media. get_content_type ( ) == ContentType :: Video {
11888 bot. send_video ( chat_id,
11989 InputFile :: url (
12090 Url :: parse ( media. get_content_urls ( ) [ 0 ]
12191 . as_str ( ) )
12292 . unwrap ( )
12393 ) )
124- . caption ( media . get_title ( ) )
94+ . caption ( media_title )
12595 . await
12696 . unwrap ( ) ;
12797 } else if media. get_content_type ( ) == ContentType :: Photo {
@@ -136,7 +106,7 @@ pub async fn start(bot: Bot) {
136106 ) ;
137107
138108 if !has_caption {
139- photo_media = photo_media. caption ( media . get_title ( ) ) ;
109+ photo_media = photo_media. caption ( & media_title ) ;
140110 has_caption = true ;
141111 }
142112
@@ -173,6 +143,7 @@ pub async fn start(bot: Bot) {
173143 let mut text = english;
174144
175145 if lang. eq ( "ru" ) // russia
146+ || lang. eq ( "rs" ) // serbia
176147 || lang. eq ( "ua" ) // ukraine
177148 || lang. eq ( "by" ) // belarus
178149 || lang. eq ( "kz" ) // kazakhstan
@@ -191,4 +162,83 @@ pub async fn start(bot: Bot) {
191162 . await
192163 . unwrap ( ) ;
193164 }
165+
166+ fn is_beyond_scope ( text : & String , size : u16 ) -> bool {
167+
168+ text. len ( ) >size as usize
169+
170+ }
171+
172+ fn cut_text ( text : String , size : u16 ) -> String {
173+ if is_beyond_scope ( & text, size) {
174+ let slice_size = size as usize ;
175+ text[ 0 ..slice_size] . to_string ( )
176+ } else {
177+ text
178+ }
179+ }
180+
181+ async fn update_user ( bot : & Bot , id : u64 , chat_id : ChatId , first_name : & String , user_lang : & String ) {
182+ if has_user ( id) . await . unwrap ( ) {
183+ let data_user: User = get_user ( id) . await . unwrap ( ) . unwrap ( ) ;
184+
185+ // days after last bot use
186+ let time_after_last_use = ( current_timestamp ( ) - data_user. timestamp ) /1000 /60 /60 /24 ;
187+
188+ if time_after_last_use >= 3 {
189+ send_msg (
190+ & bot,
191+ chat_id,
192+ "<b>🌟 Welcome back!</b>\n 🌟 We missed you! 😊" ,
193+ "<b>🌟 С возвращением!</b>\n 🌟 Мы рады снова видеть вас! 😊" ,
194+ user_lang
195+ ) . await ;
196+ }
197+
198+ if !data_user. name . eq ( first_name) {
199+
200+ set_data ( id, UserColumn :: Name , first_name)
201+ . await
202+ . unwrap ( ) ;
203+
204+ }
205+
206+ let data_chat_id = match get_data ( id, UserColumn :: ChatId ) . await {
207+ Ok ( chat_id) => chat_id,
208+ _ => {
209+ None
210+ }
211+ } ;
212+
213+ if data_chat_id. is_none ( ) {
214+
215+ set_data ( id, UserColumn :: ChatId , chat_id. 0 . to_string ( ) . as_str ( ) )
216+ . await
217+ . unwrap ( ) ;
218+
219+ }
220+
221+ } else {
222+ let data_user = User {
223+ id,
224+ chat_id : Some ( chat_id. 0 ) ,
225+ name : first_name. to_string ( ) ,
226+ requests_amount : 1 ,
227+ timestamp : current_timestamp ( ) ,
228+ register_timestamp : current_timestamp ( ) ,
229+ } ;
230+
231+ add_user ( data_user) . await . unwrap ( ) ;
232+
233+ // wellcome message
234+ send_msg (
235+ & bot,
236+ chat_id,
237+ "<b>🎉 Welcome aboard!</b>\n 🎉 It looks like you're new here. Enjoy our bot for free - no limits! 😊" ,
238+ "<b>🎉 Добро пожаловать!</b>\n 🎉 Похоже, вы здесь впервые. Пользуйтесь ботом бесплатно - без ограничений! 😊" ,
239+ user_lang
240+ ) . await ;
241+
242+ }
243+ }
194244}
0 commit comments