@@ -5,7 +5,7 @@ defmodule SaveIt.Bot do
5
5
alias SaveIt.GoogleDrive
6
6
alias SaveIt.GoogleOAuth2DeviceFlow
7
7
8
- alias SaveIt.TypesensePhoto
8
+ alias SaveIt.PhotoService
9
9
10
10
alias SmallSdk.Telegram
11
11
@@ -115,6 +115,10 @@ defmodule SaveIt.Bot do
115
115
end
116
116
end
117
117
118
+ def handle ( { :command , :search , % { chat: chat , text: nil } } , _context ) do
119
+ send_message ( chat . id , "What do you want to search? animal, food, etc." )
120
+ end
121
+
118
122
def handle ( { :command , :search , % { chat: chat , text: text } } , _context )
119
123
when is_binary ( text ) do
120
124
q = String . trim ( text )
@@ -124,7 +128,7 @@ defmodule SaveIt.Bot do
124
128
send_message ( chat . id , "What do you want to search? animal, food, etc." )
125
129
126
130
_ ->
127
- photos = TypesensePhoto . search_photos! ( q , belongs_to_id: chat . id )
131
+ photos = PhotoService . search_photos! ( q , belongs_to_id: chat . id )
128
132
129
133
answer_photos ( chat . id , photos )
130
134
end
@@ -134,30 +138,25 @@ defmodule SaveIt.Bot do
134
138
send_message ( chat . id , "Upload a photo to find similar photos." )
135
139
end
136
140
137
- # dev-notes: never reach here, it will be handled by handle({:message, %{chat: chat, caption: nil, photo: photos}}, ctx)
138
- # def handle({:command, :similar, %{chat: _chat, photo: photo}}, _context) do
139
- # end
140
-
141
141
# caption: nil -> find same photos
142
- def handle ( { :message , % { chat: chat , caption: nil , photo: photos } } , ctx ) do
142
+ def handle ( { :message , % { chat: chat , caption: nil , photo: photos } } , _ctx ) do
143
143
photo = List . last ( photos )
144
144
145
145
file = ExGram . get_file! ( photo . file_id )
146
146
photo_file_content = Telegram . download_file_content! ( file . file_path )
147
147
148
- bot_id = ctx . bot_info . id
149
148
chat_id = chat . id
150
149
151
150
typesense_photo =
152
- TypesensePhoto . create_photo! ( % {
151
+ PhotoService . create_photo! ( % {
153
152
image: Base . encode64 ( photo_file_content ) ,
154
153
caption: "" ,
155
- url: photo_url ( bot_id , file . file_id ) ,
154
+ file_id: file . file_id ,
156
155
belongs_to_id: chat_id
157
156
} )
158
157
159
158
photos =
160
- TypesensePhoto . search_similar_photos! (
159
+ PhotoService . search_similar_photos! (
161
160
typesense_photo [ "id" ] ,
162
161
distance_threshold: 0.1 ,
163
162
belongs_to_id: chat_id
@@ -170,13 +169,12 @@ defmodule SaveIt.Bot do
170
169
end
171
170
172
171
# caption: contains /similar or /search -> search similar photos; otherwise, find same photos
173
- def handle ( { :message , % { chat: chat , caption: caption , photo: photos } } , ctx ) do
172
+ def handle ( { :message , % { chat: chat , caption: caption , photo: photos } } , _ctx ) do
174
173
photo = List . last ( photos )
175
174
176
175
file = ExGram . get_file! ( photo . file_id )
177
176
photo_file_content = Telegram . download_file_content! ( file . file_path )
178
177
179
- bot_id = ctx . bot_info . id
180
178
chat_id = chat . id
181
179
182
180
caption =
@@ -187,17 +185,17 @@ defmodule SaveIt.Bot do
187
185
end
188
186
189
187
typesense_photo =
190
- TypesensePhoto . create_photo! ( % {
188
+ PhotoService . create_photo! ( % {
191
189
image: Base . encode64 ( photo_file_content ) ,
192
190
caption: caption ,
193
- url: photo_url ( bot_id , file . file_id ) ,
191
+ file_id: file . file_id ,
194
192
belongs_to_id: chat_id
195
193
} )
196
194
197
195
case caption do
198
196
"" ->
199
197
photos =
200
- TypesensePhoto . search_similar_photos! (
198
+ PhotoService . search_similar_photos! (
201
199
typesense_photo [ "id" ] ,
202
200
distance_threshold: 0.4 ,
203
201
belongs_to_id: chat_id
@@ -207,7 +205,7 @@ defmodule SaveIt.Bot do
207
205
208
206
_ ->
209
207
photos =
210
- TypesensePhoto . search_similar_photos! (
208
+ PhotoService . search_similar_photos! (
211
209
typesense_photo [ "id" ] ,
212
210
distance_threshold: 0.1 ,
213
211
belongs_to_id: chat_id
@@ -310,18 +308,15 @@ defmodule SaveIt.Bot do
310
308
end
311
309
end
312
310
313
- def handle (
314
- { :update ,
315
- % ExGram.Model.Update { message: nil , edited_message: nil , channel_post: _channel_post } } ,
316
- _context
317
- ) do
318
- Logger . warning ( "this is a channel post, ignore it" )
311
+ def handle ( { :edited_message , % { photo: nil } } , _context ) do
312
+ Logger . warning ( "this is an edited message, ignore it" )
313
+ # TODO: edit /search trigger re-search
319
314
{ :ok , nil }
320
315
end
321
316
322
- def handle ( { :edited_message , _msg } , _context ) do
323
- Logger . warning ( "this is an edited message, ignore it" )
324
- { :ok , nil }
317
+ def handle ( { :edited_message , % { chat: chat , caption: caption , photo: photos } } , _context ) do
318
+ file_id = photos |> List . last ( ) |> Map . get ( :file_id )
319
+ PhotoService . update_photo_caption! ( file_id , chat . id , caption )
325
320
end
326
321
327
322
def handle ( { :update , _update } , _context ) do
@@ -334,19 +329,6 @@ defmodule SaveIt.Bot do
334
329
{ :ok , nil }
335
330
end
336
331
337
- defp pick_file_id_from_photo_url ( photo_url ) do
338
- captures =
339
- Regex . named_captures ( ~r" /files/(?<bot_id>\d +)/(?<file_id>.+)" , photo_url )
340
-
341
- if captures == nil do
342
- Logger . error ( "Invalid photo URL: #{ photo_url } " )
343
- nil
344
- else
345
- % { "file_id" => file_id } = captures
346
- file_id
347
- end
348
- end
349
-
350
332
defp answer_photos ( chat_id , nil ) do
351
333
send_message ( chat_id , "No photos found." )
352
334
end
@@ -360,8 +342,8 @@ defmodule SaveIt.Bot do
360
342
Enum . map ( similar_photos , fn photo ->
361
343
% ExGram.Model.InputMediaPhoto {
362
344
type: "photo" ,
363
- media: pick_file_id_from_photo_url ( photo [ "url" ] ) ,
364
- caption: "Found photos" ,
345
+ media: photo [ "file_id" ] ,
346
+ caption: photo [ "caption" ] ,
365
347
show_caption_above_media: true
366
348
}
367
349
end )
@@ -414,19 +396,19 @@ defmodule SaveIt.Bot do
414
396
Enum . each ( filenames , fn filename -> bot_send_file ( chat_id , filename , { :file , filename } ) end )
415
397
end
416
398
417
- defp bot_send_file ( chat_id , file_name , file_content , _opts \\ [ ] ) do
399
+ defp bot_send_file ( chat_id , file_name , file_content , opts \\ [ ] ) do
418
400
content =
419
401
case file_content do
420
402
{ :file , file } -> { :file , file }
421
403
{ :file_content , file_content , file_name } -> { :file_content , file_content , file_name }
422
404
end
423
405
424
- # caption = opts[ :caption]
406
+ caption = Keyword . get ( opts , :caption , "" )
425
407
426
408
case file_extension ( file_name ) do
427
409
ext when ext in [ ".png" , ".jpg" , ".jpeg" ] ->
428
- { :ok , msg } = ExGram . send_photo ( chat_id , content )
429
- bot_id = msg . from . id
410
+ { :ok , msg } = ExGram . send_photo ( chat_id , content , caption: caption )
411
+
430
412
file_id = get_file_id ( msg )
431
413
432
414
image_base64 =
@@ -435,21 +417,21 @@ defmodule SaveIt.Bot do
435
417
{ :file_content , file_content , _file_name } -> Base . encode64 ( file_content )
436
418
end
437
419
438
- TypesensePhoto . create_photo! ( % {
420
+ PhotoService . create_photo! ( % {
439
421
image: image_base64 ,
440
- caption: file_name ,
441
- url: photo_url ( bot_id , file_id ) ,
422
+ caption: caption ,
423
+ file_id: file_id ,
442
424
belongs_to_id: chat_id
443
425
} )
444
426
445
427
".mp4" ->
446
- ExGram . send_video ( chat_id , content , supports_streaming: true )
428
+ ExGram . send_video ( chat_id , content , supports_streaming: true , caption: caption )
447
429
448
430
".gif" ->
449
- ExGram . send_animation ( chat_id , content )
431
+ ExGram . send_animation ( chat_id , content , caption: caption )
450
432
451
433
_ ->
452
- ExGram . send_document ( chat_id , content )
434
+ ExGram . send_document ( chat_id , content , caption: caption )
453
435
end
454
436
end
455
437
@@ -487,12 +469,4 @@ defmodule SaveIt.Bot do
487
469
""" )
488
470
end
489
471
end
490
-
491
- defp photo_url ( bot_id , file_id ) do
492
- proxy_url = Application . fetch_env! ( :save_it , :web_url ) <> "/telegram/files"
493
-
494
- encoded_bot_id = URI . encode ( bot_id |> to_string ( ) )
495
- encoded_file_id = URI . encode ( file_id )
496
- "#{ proxy_url } /#{ encoded_bot_id } /#{ encoded_file_id } "
497
- end
498
472
end
0 commit comments