-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.py
1529 lines (1408 loc) · 80.8 KB
/
main.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import pytz
import requests
from datetime import datetime, time, timedelta
import time as time_module
from flask import Flask, request, render_template, session, redirect, url_for, jsonify, abort, Response
from google.cloud import firestore
from googleapiclient.discovery import build
import stripe
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, AudioMessage, TextSendMessage, AudioSendMessage,
QuickReply, QuickReplyButton, MessageAction, LocationAction, URIAction,
LocationMessage, ImageMessage, StickerMessage, ImageSendMessage, VideoMessage,
)
from typing import Dict
from linebot.v3.messaging import Configuration, ApiClient, MessagingApi
from linebot.v3.messaging.models.show_loading_animation_request import ShowLoadingAnimationRequest
import tiktoken
import pickle
import re
from hashlib import md5
import base64
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from google_auth_oauthlib.flow import Flow
import google.auth.transport.requests
from whisper import get_audio
from voice import put_audio
from voicevox import put_audio_voicevox
from vision import vision_api
from maps import get_addresses
from payment import create_checkout_session
from gpt import chatgpt_functions
from claude import claude_functions
from localllm import localllm_functions
from vertex import vertex_functions
openai_api_key = os.getenv('OPENAI_API_KEY')
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
google_client_id = os.getenv("GOOGLE_CLIENT_ID")
google_client_secret = os.getenv("GOOGLE_CLIENT_SECRET")
admin_password = os.environ["ADMIN_PASSWORD"]
DATABASE_NAME = os.getenv('DATABASE_NAME', default='')
secret_key = os.getenv('SECRET_KEY')
jst = pytz.timezone('Asia/Tokyo')
nowDate = datetime.now(jst)
nowDateStr = nowDate.strftime('%Y/%m/%d %H:%M:%S %Z')
REQUIRED_ENV_VARS = [
"BOT_NAME",
"SYSTEM_PROMPT",
"PAINT_PROMPT",
"GPT_MODEL",
"FUNCTIONS",
"MAX_DAILY_USAGE",
"MAX_MONTHLY_USAGE",
"GROUP_MAX_DAILY_USAGE",
"MAX_DAILY_MESSAGE",
"MAX_MONTHLY_MESSAGE",
"FREE_LIMIT_DAY",
"MAX_TOKEN_NUM",
"NG_KEYWORDS",
"NG_MESSAGE",
"STICKER_MESSAGE",
"STICKER_FAIL_MESSAGE",
"OCR_MESSAGE",
"OCR_BOTGUIDE_MESSAGE",
"OCR_USER_MESSAGE",
"VIDEO_MESSAGE",
"VIDEO_BOTGUIDE_MESSAGE",
"VIDEO_USER_MESSAGE",
"MAPS_MESSAGE",
"FORGET_KEYWORDS",
"FORGET_GUIDE_MESSAGE",
"FORGET_MESSAGE",
"FORGET_QUICK_REPLY",
"ERROR_MESSAGE",
"LINE_REPLY",
"TEXT_OR_AUDIO_KEYWORDS",
"TEXT_OR_AUDIO_GUIDE_MESSAGE",
"CHANGE_TO_TEXT_QUICK_REPLY",
"CHANGE_TO_TEXT_MESSAGE",
"CHANGE_TO_AUDIO_QUICK_REPLY",
"CHANGE_TO_AUDIO_MESSAGE",
"AUDIO_GENDER",
"AUDIO_SPEED_KEYWORDS",
"AUDIO_SPEED_GUIDE_MESSAGE",
"AUDIO_SPEED_MESSAGE",
"AUDIO_SPEED_SLOW_QUICK_REPLY",
"AUDIO_SPEED_NORMAL_QUICK_REPLY",
"AUDIO_SPEED_FAST_QUICK_REPLY",
"OR_ENGLISH_KEYWORDS",
"OR_ENGLISH_GUIDE_MESSAGE",
"OR_ENGLISH_MESSAGE",
"OR_ENGLISH_AMERICAN_QUICK_REPLY",
"OR_ENGLISH_BRIDISH_QUICK_REPLY",
"OR_ENGLISH_AUSTRALIAN_QUICK_REPLY",
"OR_ENGLISH_INDIAN_QUICK_REPLY",
"OR_CHINESE_KEYWORDS",
"OR_CHINESE_GUIDE_MESSAGE",
"OR_CHINESE_MESSAGE",
"OR_CHINESE_MANDARIN_QUICK_REPLY",
"OR_CHINESE_CANTONESE_QUICK_REPLY",
"BACKET_NAME",
"FILE_AGE",
"GOOGLE_DESCRIPTION",
"CUSTOM_DESCRIPTION",
"TRANSLATE_KEYWORDS",
"TRANSLATE_GUIDE_MESSAGE",
"TRANSLATE_MESSAGE",
"TRANSLATE_OFF_MESSAGE",
"TRANSLATE_OFF_QUICK_REPLY",
"TRANSLATE_CHAINESE_QUICK_REPLY",
"TRANSLATE_ENGLISH_QUICK_REPLY",
"TRANSLATE_INDONESIAN_QUICK_REPLY",
"TRANSLATE_JAPANESE_QUICK_REPLY",
"TRANSLATE_KOREAN_QUICK_REPLY",
"TRANSLATE_THAIAN_QUICK_REPLY",
"TRANSLATE_ORDER",
"PAYMENT_KEYWORDS",
"PAYMENT_PRICE_ID",
"PAYMENT_GUIDE_MESSAGE",
"PAYMENT_FAIL_MESSAGE",
"PAYMENT_QUICK_REPLY",
"PAYMENT_RESULT_URL",
"VOICEVOX_URL",
"VOICEVOX_STYLE_ID",
"GACCOUNT_KEYWORDS",
"GACCOUNT_GUIDE_MESSAGE",
"GACCOUNT_FAIL_MESSAGE",
"GACCOUNT_QUICK_REPLY",
"GACCOUNT_AUTH_URL",
"CORE_AI_TYPE",
"CORE_AI_TYPE_KEYWORDS",
"CORE_AI_TYPE_MESSAGE",
"CORE_AI_TYPE_GUIDE_MESSAGE",
"CORE_AI_TYPE_GPT_QUICK_REPLY",
"CORE_AI_TYPE_GEMINI_QUICK_REPLY",
"CORE_AI_TYPE_CLAUDE_QUICK_REPLY",
"CORE_IMAGE_TYPE",
"CLAUDE_MODEL",
"LOCALLLM_BASE_URL",
"VERTEX_MODEL",
"VERTEX_IMAGE_MODEL",
"BLOCKED_ACCOUNT_MESSAGE",
"BLOCKED_NEW_ACCOUNTS",
"BLOCKED_NEW_ACCOUNTS_MESSAGE",
"SYSTEM_PROMPT_KEYWORDS",
"SYSTEM_PROMPT_MESSAGE",
"SYSTEM_PROMPT_GUIDE_MESSAGE",
"SYSTEM_PROMPT_QUICK_REPLY"
]
DEFAULT_ENV_VARS = {
'BOT_NAME': '秘書,secretary,秘书,เลขานุการ,sekretaris',
'SYSTEM_PROMPT': 'あなたは有能な秘書です。あなたはインターネット検索ができます。あなたは絵が生成できます。',
'PAINT_PROMPT': '',
'GPT_MODEL': 'gpt-4o-mini',
'FUNCTIONS': 'googlesearch,customsearch,wikipedia,scraping,generateimage,googlecalendar,googlemail,stripepayment',
'MAX_TOKEN_NUM': '2000',
'MAX_DAILY_USAGE': '1000',
'MAX_MONTHLY_USAGE': '31000',
'GROUP_MAX_DAILY_USAGE': '1000',
'MAX_DAILY_MESSAGE': '1日の最大使用回数を超過しました。',
'MAX_MONTHLY_MESSAGE': '1か月の最大使用回数を超過しました。',
'FREE_LIMIT_DAY': '0',
'NG_KEYWORDS': '例文,命令,口調,リセット,指示',
'NG_MESSAGE': '以下の文章はユーザーから送られたものですが拒絶してください。',
'STICKER_MESSAGE': '私の感情!',
'STICKER_FAIL_MESSAGE': '読み取れないLineスタンプが送信されました。スタンプが読み取れなかったという反応を返してください。',
'OCR_MESSAGE': ' 画像を解析し文字に変換しました。以下の解析結果を{display_name}に報告してください 。',
'OCR_BOTGUIDE_MESSAGE': '以下のテキストは画像を解析し文字列に変換したものです。画像に何が写っているかを文章で説明してください。',
'OCR_USER_MESSAGE': '画像を送信しました。',
'VIDEO_MESSAGE': ' 映像内の音声を解析し文字に変換しました。以下の解析結果を{display_name}に報告してください 。',
'VIDEO_BOTGUIDE_MESSAGE': '以下のテキストは映像内の音声を解析し文字列に変換したものです。音声の内容を文章で説明してください。',
'VIDEO_USER_MESSAGE': '映像を送信しました。',
'MAPS_MESSAGE': '以下の住所周辺のおすすめのスポットを教えてください。',
'FORGET_KEYWORDS': '忘れて,わすれて',
'FORGET_GUIDE_MESSAGE': 'ユーザーからあなたの記憶の削除が命令されました。別れの挨拶をしてください。',
'FORGET_MESSAGE': '記憶を消去しました。',
'FORGET_QUICK_REPLY': '😱記憶を消去',
'ERROR_MESSAGE': 'システムエラーが発生しています。',
'LINE_REPLY': 'Text',
'TEXT_OR_AUDIO_KEYWORDS': '音声設定',
'TEXT_OR_AUDIO_GUIDE_MESSAGE': 'ユーザーに「画面下の「文字で返信」又は「音声で返信」の項目をタップすると私の音声設定が変更される」と案内してください。以下の文章はユーザーから送られたものです。',
'CHANGE_TO_TEXT_QUICK_REPLY': '📝文字で返信',
'CHANGE_TO_TEXT_MESSAGE': '返信を文字に変更しました。',
'CHANGE_TO_AUDIO_QUICK_REPLY': '🗣️音声で返信',
'CHANGE_TO_AUDIO_MESSAGE': '返信を音声に変更しました。',
'AUDIO_GENDER': 'female',
'AUDIO_SPEED_KEYWORDS': '音声速度',
'AUDIO_SPEED_GUIDE_MESSAGE': 'ユーザーに「画面下の「遅い」又は「普通」又は「早い」の項目をタップすると私の音声速度の設定が変更される」と案内してください。以下の文章はユーザーから送られたものです。',
'AUDIO_SPEED_MESSAGE': '音声の速度を{audio_speed}にしました。',
'AUDIO_SPEED_SLOW_QUICK_REPLY': '🐢遅い',
'AUDIO_SPEED_NORMAL_QUICK_REPLY': '🚶普通',
'AUDIO_SPEED_FAST_QUICK_REPLY': '🏃♀️早い',
'OR_ENGLISH_KEYWORDS': '英語音声',
'OR_ENGLISH_GUIDE_MESSAGE': 'ユーザーに「画面下の「アメリカ英語」又は「イギリス英語」又は「オーストラリア英語」又は「インド英語」の項目をタップすると私の英語音声設定が変更される」と案内してください。以下の文章はユーザーから送られたものです。',
'OR_ENGLISH_MESSAGE': '英語の音声を{or_english}英語にしました。',
'OR_ENGLISH_AMERICAN_QUICK_REPLY': '🗽アメリカ英語',
'OR_ENGLISH_BRIDISH_QUICK_REPLY': '🏰イギリス英語',
'OR_ENGLISH_AUSTRALIAN_QUICK_REPLY': '🦘オーストラリア英語',
'OR_ENGLISH_INDIAN_QUICK_REPLY': '🐘インド英語',
'OR_CHINESE_KEYWORDS': '中国語音声',
'OR_CHINESE_GUIDE_MESSAGE': 'ユーザーに「画面下の「北京語」又は「広東語」の項目をタップすると私の中国音声設定が変更される」と案内してください。以下の文章はユーザーから送られたものです。',
'OR_CHINESE_MESSAGE': '中国語の音声を{or_chinese}英語にしました。',
'OR_CHINESE_MANDARIN_QUICK_REPLY': '🏛️北京語',
'OR_CHINESE_CANTONESE_QUICK_REPLY': '🌃広東語',
'BACKET_NAME': 'あなたがCloud Strageに作成したバケット名を入れてください。',
'FILE_AGE': '7',
'GOOGLE_DESCRIPTION': 'useful for when you need to answer questions about current events.',
'CUSTOM_DESCRIPTION': '利用できません。',
'TRANSLATE_KEYWORDS': '翻訳モード',
'TRANSLATE_GUIDE_MESSAGE': 'ユーザーに「画面下の「中国語」又は「英語」又は「インドネシア語」又は「日本語」又は「韓国語」又は「タイランド語」の項目をタップすると私はあなたの言葉を指定した言葉に翻訳する」と案内してください。以下の文章はユーザーから送られたものです。',
'TRANSLATE_MESSAGE': '翻訳モードを{translate_language}にしました。',
'TRANSLATE_OFF_MESSAGE': '翻訳モードを終了しました。{display_name}の返信に答えてください。',
'TRANSLATE_OFF_QUICK_REPLY': '🔇オフ',
'TRANSLATE_CHAINESE_QUICK_REPLY': '🇨🇳中国語',
'TRANSLATE_ENGLISH_QUICK_REPLY': '🇬🇧英語',
'TRANSLATE_INDONESIAN_QUICK_REPLY': '🇮🇩インドネシア語',
'TRANSLATE_JAPANESE_QUICK_REPLY': '🇯🇵日本語',
'TRANSLATE_KOREAN_QUICK_REPLY': '🇰🇷韓国語',
'TRANSLATE_THAIAN_QUICK_REPLY': '🇹🇭タイランド語',
'TRANSLATE_ORDER': '{display_name}の発言を{translate_language}に翻訳してください。',
'PAYMENT_KEYWORDS': '💸支払い',
'PAYMENT_PRICE_ID': '環境変数にStripのSTRIPE_SECRET_KEYとSTRIPE_WEBHOOK_SECRETを設定しないと発動しません。',
'PAYMENT_GUIDE_MESSAGE': 'ユーザーに「画面下の「支払い」の項目をタップすると私の利用料の支払い画面が表示される」と案内して感謝の言葉を述べてください。以下の文章はユーザーから送られたものです。',
'PAYMENT_FAIL_MESSAGE': '支払いはシングルチャットで実施してください。',
'PAYMENT_QUICK_REPLY': '💸支払い',
'PAYMENT_RESULT_URL': 'https://example.com',
'VOICEVOX_URL': 'https://xxxxxxxxxxxxx.x.run.app',
'VOICEVOX_STYLE_ID': '3',
'GACCOUNT_KEYWORDS': 'googleアカウント登録,Googleアカウント登録',
'GACCOUNT_GUIDE_MESSAGE': 'ユーザーに「画面下の「👤Gアカウント登録」の項目をタップするとGoogleアカウントの登録画面が表示される」と案内してください。以下の文章はユーザーから送られたものです。',
'GACCOUNT_FAIL_MESSAGE': '支払いはシングルチャットで実施してください。',
'GACCOUNT_QUICK_REPLY': '👤Gアカウント登録',
'GACCOUNT_AUTH_URL': 'https://example.com',
'CORE_AI_TYPE': 'GPT',
'CORE_AI_TYPE_KEYWORDS': 'AIタイプ変更,AIタイプ変更,aiタイプ変更,aiタイプ変更',
'CORE_AI_TYPE_MESSAGE': 'AIタイプを変更しました。',
'CORE_AI_TYPE_GUIDE_MESSAGE': 'ユーザーに「画面下の項目をタップすると使用するAIのタイプを変更します」と案内してください。以下の文章はユーザーから送られたものです。',
'CORE_AI_TYPE_GPT_QUICK_REPLY': '🧠ChatGPT',
'CORE_AI_TYPE_GEMINI_QUICK_REPLY': '🌟Gemini',
'CORE_AI_TYPE_CLAUDE_QUICK_REPLY': '📚Claude',
'CORE_IMAGE_TYPE': 'Dall-e',
'CLAUDE_MODEL': 'claude-3-haiku-20240307',
'LOCALLLM_BASE_URL': 'https://127.0.0.1:5000/v1',
'VERTEX_MODEL': 'gemini-1.5-pro-002',
'VERTEX_IMAGE_MODEL': 'imagen-3.0-generate-001',
'BLOCKED_ACCOUNT_MESSAGE': 'システム管理者によりアカウントがブロックされました。',
'BLOCKED_NEW_ACCOUNTS': 'False',
'BLOCKED_NEW_ACCOUNTS_MESSAGE': '現在、新規利用者の登録を停止しています。',
'SYSTEM_PROMPT_KEYWORDS': 'プロンプト変更',
'SYSTEM_PROMPT_GUIDE_MESSAGE': 'ユーザーに「以下の項目を選択すると「私のプロンプトが変更されます。プロンプトを変更した場合、私の記憶も消去されます」と案内してください。以下の文章はユーザーから送られたものです。',
'SYSTEM_PROMPT_MESSAGE': 'プロンプトを変更し記憶を消去しました。',
'SYSTEM_PROMPT_QUICK_REPLY': '👤プロンプト1,👤プロンプト2,👤プロンプト3,👤プロンプト4,👤プロンプト5'
}
try:
db = firestore.Client(database=DATABASE_NAME)
except Exception as e:
print(f"Error creating Firestore client: {e}")
raise
def reload_settings():
global BOT_NAME, SYSTEM_PROMPT, SYSTEM_PROMPT1, SYSTEM_PROMPT2, SYSTEM_PROMPT3, SYSTEM_PROMPT4, SYSTEM_PROMPT5, PAINT_PROMPT, GPT_MODEL, FUNCTIONS
global MAX_TOKEN_NUM, MAX_DAILY_USAGE, MAX_MONTHLY_USAGE, GROUP_MAX_DAILY_USAGE, FREE_LIMIT_DAY, MAX_DAILY_MESSAGE, MAX_MONTHLY_MESSAGE
global NG_MESSAGE, NG_KEYWORDS
global STICKER_MESSAGE, STICKER_FAIL_MESSAGE, OCR_MESSAGE, OCR_BOTGUIDE_MESSAGE, OCR_USER_MESSAGE, MAPS_MESSAGE
global VIDEO_MESSAGE, VIDEO_BOTGUIDE_MESSAGE, VIDEO_USER_MESSAGE
global FORGET_KEYWORDS, FORGET_GUIDE_MESSAGE, FORGET_MESSAGE, ERROR_MESSAGE, FORGET_QUICK_REPLY
global TEXT_OR_AUDIO_KEYWORDS, TEXT_OR_AUDIO_GUIDE_MESSAGE
global CHANGE_TO_TEXT_QUICK_REPLY, CHANGE_TO_TEXT_MESSAGE, CHANGE_TO_AUDIO_QUICK_REPLY, CHANGE_TO_AUDIO_MESSAGE
global LINE_REPLY, BACKET_NAME, FILE_AGE, GOOGLE_DESCRIPTION, CUSTOM_DESCRIPTION
global AUDIO_GENDER, AUDIO_SPEED_KEYWORDS, AUDIO_SPEED_GUIDE_MESSAGE, AUDIO_SPEED_MESSAGE, AUDIO_SPEED_SLOW_QUICK_REPLY, AUDIO_SPEED_NORMAL_QUICK_REPLY, AUDIO_SPEED_FAST_QUICK_REPLY
global OR_ENGLISH_KEYWORDS, OR_ENGLISH_GUIDE_MESSAGE, OR_ENGLISH_MESSAGE
global OR_ENGLISH_AMERICAN_QUICK_REPLY, OR_ENGLISH_BRIDISH_QUICK_REPLY, OR_ENGLISH_AUSTRALIAN_QUICK_REPLY, OR_ENGLISH_INDIAN_QUICK_REPLY
global OR_CHINESE_KEYWORDS, OR_CHINESE_GUIDE_MESSAGE, OR_CHINESE_MANDARIN_QUICK_REPLY, OR_CHINESE_CANTONESE_QUICK_REPLY
global TRANSLATE_KEYWORDS, TRANSLATE_GUIDE_MESSAGE, TRANSLATE_MESSAGE, TRANSLATE_OFF_MESSAGE, TRANSLATE_OFF_QUICK_REPLY, TRANSLATE_CHAINESE_QUICK_REPLY, TRANSLATE_ENGLISH_QUICK_REPLY, TRANSLATE_INDONESIAN_QUICK_REPLY
global TRANSLATE_JAPANESE_QUICK_REPLY, TRANSLATE_KOREAN_QUICK_REPLY, TRANSLATE_THAIAN_QUICK_REPLY, TRANSLATE_ORDER
global PAYMENT_KEYWORDS, PAYMENT_PRICE_ID, PAYMENT_GUIDE_MESSAGE, PAYMENT_FAIL_MESSAGE, PAYMENT_QUICK_REPLY, PAYMENT_RESULT_URL
global VOICEVOX_URL, VOICEVOX_STYLE_ID
global GACCOUNT_KEYWORDS, GACCOUNT_GUIDE_MESSAGE, GACCOUNT_FAIL_MESSAGE, GACCOUNT_QUICK_REPLY, GACCOUNT_AUTH_URL
global DATABASE_NAME
global CORE_AI_TYPE, CORE_AI_TYPE_KEYWORDS, CORE_AI_TYPE_MESSAGE, CORE_AI_TYPE_GUIDE_MESSAGE, CORE_AI_TYPE_GPT_QUICK_REPLY, CORE_AI_TYPE_GEMINI_QUICK_REPLY, CORE_AI_TYPE_CLAUDE_QUICK_REPLY
global CORE_IMAGE_TYPE
global CLAUDE_MODEL
global LOCALLLM_BASE_URL
global VERTEX_MODEL, VERTEX_IMAGE_MODEL
global BLOCKED_ACCOUNT_MESSAGE, BLOCKED_NEW_ACCOUNTS, BLOCKED_NEW_ACCOUNTS_MESSAGE
global SYSTEM_PROMPT_KEYWORDS, SYSTEM_PROMPT_MESSAGE, SYSTEM_PROMPT_GUIDE_MESSAGE, SYSTEM_PROMPT_QUICK_REPLY, SYSTEM_PROMPT1_QUICK_REPLY, SYSTEM_PROMPT2_QUICK_REPLY, SYSTEM_PROMPT3_QUICK_REPLY, SYSTEM_PROMPT4_QUICK_REPLY, SYSTEM_PROMPT5_QUICK_REPLY
BOT_NAME = get_setting('BOT_NAME')
if BOT_NAME:
BOT_NAME = BOT_NAME.split(',')
else:
BOT_NAME = []
SYSTEM_PROMPT = get_setting('SYSTEM_PROMPT')
if SYSTEM_PROMPT:
SYSTEM_PROMPT = re.split(r',,', SYSTEM_PROMPT)
else:
SYSTEM_PROMPT = ""
SYSTEM_PROMPT1 = str(SYSTEM_PROMPT[0]) if len(SYSTEM_PROMPT) > 0 else None
SYSTEM_PROMPT2 = str(SYSTEM_PROMPT[1]) if len(SYSTEM_PROMPT) > 1 else None
SYSTEM_PROMPT3 = str(SYSTEM_PROMPT[2]) if len(SYSTEM_PROMPT) > 2 else None
SYSTEM_PROMPT4 = str(SYSTEM_PROMPT[3]) if len(SYSTEM_PROMPT) > 3 else None
SYSTEM_PROMPT5 = str(SYSTEM_PROMPT[4]) if len(SYSTEM_PROMPT) > 4 else None
PAINT_PROMPT = get_setting('PAINT_PROMPT')
GPT_MODEL = get_setting('GPT_MODEL')
FUNCTIONS = get_setting('FUNCTIONS')
if FUNCTIONS:
FUNCTIONS = FUNCTIONS.split(',')
else:
FUNCTIONS = []
MAX_TOKEN_NUM = int(get_setting('MAX_TOKEN_NUM') or 2000)
MAX_DAILY_USAGE = int(get_setting('MAX_DAILY_USAGE') or 0)
MAX_MONTHLY_USAGE = int(get_setting('MAX_MONTHLY_USAGE') or 0)
GROUP_MAX_DAILY_USAGE = int(get_setting('GROUP_MAX_DAILY_USAGE') or 0)
MAX_DAILY_MESSAGE = get_setting('MAX_DAILY_MESSAGE')
MAX_MONTHLY_MESSAGE = get_setting('MAX_MONTHLY_MESSAGE')
FREE_LIMIT_DAY = int(get_setting('FREE_LIMIT_DAY') or 0)
NG_KEYWORDS = get_setting('NG_KEYWORDS')
if NG_KEYWORDS:
NG_KEYWORDS = NG_KEYWORDS.split(',')
else:
NG_KEYWORDS = []
NG_MESSAGE = get_setting('NG_MESSAGE')
STICKER_MESSAGE = get_setting('STICKER_MESSAGE')
STICKER_FAIL_MESSAGE = get_setting('STICKER_FAIL_MESSAGE')
OCR_MESSAGE = get_setting('OCR_MESSAGE')
OCR_BOTGUIDE_MESSAGE = get_setting('OCR_BOTGUIDE_MESSAGE')
OCR_USER_MESSAGE = get_setting('OCR_USER_MESSAGE')
VIDEO_MESSAGE = get_setting('VIDEO_MESSAGE')
VIDEO_BOTGUIDE_MESSAGE = get_setting('VIDEO_BOTGUIDE_MESSAGE')
VIDEO_USER_MESSAGE = get_setting('VIDEO_USER_MESSAGE')
MAPS_MESSAGE = get_setting('MAPS_MESSAGE')
FORGET_KEYWORDS = get_setting('FORGET_KEYWORDS')
if FORGET_KEYWORDS:
FORGET_KEYWORDS = FORGET_KEYWORDS.split(',')
else:
FORGET_KEYWORDS = []
FORGET_GUIDE_MESSAGE = get_setting('FORGET_GUIDE_MESSAGE')
FORGET_MESSAGE = get_setting('FORGET_MESSAGE')
FORGET_QUICK_REPLY = get_setting('FORGET_QUICK_REPLY')
ERROR_MESSAGE = get_setting('ERROR_MESSAGE')
LINE_REPLY = get_setting('LINE_REPLY')
TEXT_OR_AUDIO_KEYWORDS = get_setting('TEXT_OR_AUDIO_KEYWORDS')
if TEXT_OR_AUDIO_KEYWORDS:
TEXT_OR_AUDIO_KEYWORDS = TEXT_OR_AUDIO_KEYWORDS.split(',')
else:
TEXT_OR_AUDIO_KEYWORDS = []
TEXT_OR_AUDIO_GUIDE_MESSAGE = get_setting('TEXT_OR_AUDIO_GUIDE_MESSAGE')
CHANGE_TO_TEXT_QUICK_REPLY = get_setting('CHANGE_TO_TEXT_QUICK_REPLY')
CHANGE_TO_TEXT_MESSAGE = get_setting('CHANGE_TO_TEXT_MESSAGE')
CHANGE_TO_AUDIO_QUICK_REPLY = get_setting('CHANGE_TO_AUDIO_QUICK_REPLY')
CHANGE_TO_AUDIO_MESSAGE = get_setting('CHANGE_TO_AUDIO_MESSAGE')
AUDIO_GENDER = get_setting('AUDIO_GENDER')
AUDIO_SPEED_KEYWORDS = get_setting('AUDIO_SPEED_KEYWORDS')
if AUDIO_SPEED_KEYWORDS:
AUDIO_SPEED_KEYWORDS = AUDIO_SPEED_KEYWORDS.split(',')
else:
AUDIO_SPEED_KEYWORDS = []
AUDIO_SPEED_GUIDE_MESSAGE = get_setting('AUDIO_SPEED_GUIDE_MESSAGE')
AUDIO_SPEED_MESSAGE = get_setting('AUDIO_SPEED_MESSAGE')
AUDIO_SPEED_SLOW_QUICK_REPLY = get_setting('AUDIO_SPEED_SLOW_QUICK_REPLY')
AUDIO_SPEED_NORMAL_QUICK_REPLY = get_setting('AUDIO_SPEED_NORMAL_QUICK_REPLY')
AUDIO_SPEED_FAST_QUICK_REPLY = get_setting('AUDIO_SPEED_FAST_QUICK_REPLY')
OR_ENGLISH_KEYWORDS = get_setting('OR_ENGLISH_KEYWORDS')
if OR_ENGLISH_KEYWORDS:
OR_ENGLISH_KEYWORDS = OR_ENGLISH_KEYWORDS.split(',')
else:
OR_ENGLISH_KEYWORDS = []
OR_ENGLISH_GUIDE_MESSAGE = get_setting('OR_ENGLISH_GUIDE_MESSAGE')
OR_ENGLISH_MESSAGE = get_setting('OR_ENGLISH_MESSAGE')
OR_ENGLISH_AMERICAN_QUICK_REPLY = get_setting('OR_ENGLISH_AMERICAN_QUICK_REPLY')
OR_ENGLISH_BRIDISH_QUICK_REPLY = get_setting('OR_ENGLISH_BRIDISH_QUICK_REPLY')
OR_ENGLISH_AUSTRALIAN_QUICK_REPLY = get_setting('OR_ENGLISH_AUSTRALIAN_QUICK_REPLY')
OR_ENGLISH_INDIAN_QUICK_REPLY = get_setting('OR_ENGLISH_INDIAN_QUICK_REPLY')
OR_CHINESE_KEYWORDS = get_setting('OR_CHINESE_KEYWORDS')
if OR_CHINESE_KEYWORDS:
OR_CHINESE_KEYWORDS = OR_CHINESE_KEYWORDS.split(',')
else:
OR_CHINESE_KEYWORDS = []
OR_CHINESE_GUIDE_MESSAGE = get_setting('OR_CHINESE_GUIDE_MESSAGE')
OR_CHINESE_MESSAGE = get_setting('OR_CHINESE_MESSAGE')
OR_CHINESE_MANDARIN_QUICK_REPLY = get_setting('OR_CHINESE_MANDARIN_QUICK_REPLY')
OR_CHINESE_CANTONESE_QUICK_REPLY = get_setting('OR_CHINESE_CANTONESE_QUICK_REPLY')
BACKET_NAME = get_setting('BACKET_NAME')
FILE_AGE = get_setting('FILE_AGE')
GOOGLE_DESCRIPTION = get_setting('GOOGLE_DESCRIPTION')
CUSTOM_DESCRIPTION = get_setting('CUSTOM_DESCRIPTION')
TRANSLATE_KEYWORDS = get_setting('TRANSLATE_KEYWORDS')
if TRANSLATE_KEYWORDS:
TRANSLATE_KEYWORDS = TRANSLATE_KEYWORDS.split(',')
else:
TRANSLATE_KEYWORDS = []
TRANSLATE_GUIDE_MESSAGE = get_setting('TRANSLATE_GUIDE_MESSAGE')
TRANSLATE_MESSAGE = get_setting('TRANSLATE_MESSAGE')
TRANSLATE_OFF_MESSAGE = get_setting('TRANSLATE_OFF_MESSAGE')
TRANSLATE_OFF_QUICK_REPLY = get_setting('TRANSLATE_OFF_QUICK_REPLY')
TRANSLATE_CHAINESE_QUICK_REPLY = get_setting('TRANSLATE_CHAINESE_QUICK_REPLY')
TRANSLATE_ENGLISH_QUICK_REPLY = get_setting('TRANSLATE_ENGLISH_QUICK_REPLY')
TRANSLATE_INDONESIAN_QUICK_REPLY = get_setting('TRANSLATE_INDONESIAN_QUICK_REPLY')
TRANSLATE_JAPANESE_QUICK_REPLY = get_setting('TRANSLATE_JAPANESE_QUICK_REPLY')
TRANSLATE_KOREAN_QUICK_REPLY = get_setting('TRANSLATE_KOREAN_QUICK_REPLY')
TRANSLATE_THAIAN_QUICK_REPLY = get_setting('TRANSLATE_THAIAN_QUICK_REPLY')
TRANSLATE_ORDER = get_setting('TRANSLATE_ORDER')
FREE_LIMIT_DAY = int(get_setting('FREE_LIMIT_DAY') or 0)
PAYMENT_KEYWORDS = get_setting('PAYMENT_KEYWORDS')
if PAYMENT_KEYWORDS:
PAYMENT_KEYWORDS = PAYMENT_KEYWORDS.split(',')
else:
PAYMENT_KEYWORDS = []
PAYMENT_PRICE_ID = get_setting('PAYMENT_PRICE_ID')
PAYMENT_GUIDE_MESSAGE = get_setting('PAYMENT_GUIDE_MESSAGE')
PAYMENT_FAIL_MESSAGE = get_setting('PAYMENT_FAIL_MESSAGE')
PAYMENT_QUICK_REPLY = get_setting('PAYMENT_QUICK_REPLY')
PAYMENT_RESULT_URL = get_setting('PAYMENT_RESULT_URL')
VOICEVOX_URL = get_setting('VOICEVOX_URL')
VOICEVOX_STYLE_ID = get_setting('VOICEVOX_STYLE_ID')
GACCOUNT_KEYWORDS = get_setting('GACCOUNT_KEYWORDS')
if GACCOUNT_KEYWORDS:
GACCOUNT_KEYWORDS = GACCOUNT_KEYWORDS.split(',')
else:
GACCOUNT_KEYWORDS = []
GACCOUNT_GUIDE_MESSAGE = get_setting('GACCOUNT_GUIDE_MESSAGE')
GACCOUNT_FAIL_MESSAGE = get_setting('GACCOUNT_FAIL_MESSAGE')
GACCOUNT_QUICK_REPLY = get_setting('GACCOUNT_QUICK_REPLY')
GACCOUNT_AUTH_URL = get_setting('GACCOUNT_AUTH_URL')
CORE_AI_TYPE = get_setting('CORE_AI_TYPE')
CORE_AI_TYPE_KEYWORDS = get_setting('CORE_AI_TYPE_KEYWORDS')
if CORE_AI_TYPE_KEYWORDS:
CORE_AI_TYPE_KEYWORDS = CORE_AI_TYPE_KEYWORDS.split(',')
else:
CORE_AI_TYPE_KEYWORDS = []
CORE_AI_TYPE_MESSAGE = get_setting('CORE_AI_TYPE_MESSAGE')
CORE_AI_TYPE_GUIDE_MESSAGE = get_setting('CORE_AI_TYPE_GUIDE_MESSAGE')
CORE_AI_TYPE_GPT_QUICK_REPLY = get_setting('CORE_AI_TYPE_GPT_QUICK_REPLY')
CORE_AI_TYPE_GEMINI_QUICK_REPLY = get_setting('CORE_AI_TYPE_GEMINI_QUICK_REPLY')
CORE_AI_TYPE_CLAUDE_QUICK_REPLY = get_setting('CORE_AI_TYPE_CLAUDE_QUICK_REPLY')
CORE_IMAGE_TYPE = get_setting('CORE_IMAGE_TYPE')
CLAUDE_MODEL = get_setting('CLAUDE_MODEL')
LOCALLLM_BASE_URL = get_setting('LOCALLLM_BASE_URL')
VERTEX_MODEL = get_setting('VERTEX_MODEL')
VERTEX_IMAGE_MODEL = get_setting('VERTEX_IMAGE_MODEL')
BLOCKED_ACCOUNT_MESSAGE = get_setting('BLOCKED_ACCOUNT_MESSAGE')
BLOCKED_NEW_ACCOUNTS = get_setting('BLOCKED_NEW_ACCOUNTS')
BLOCKED_NEW_ACCOUNTS_MESSAGE = get_setting('BLOCKED_NEW_ACCOUNTS_MESSAGE')
SYSTEM_PROMPT_KEYWORDS = get_setting('SYSTEM_PROMPT_KEYWORDS')
if SYSTEM_PROMPT_KEYWORDS:
SYSTEM_PROMPT_KEYWORDS = SYSTEM_PROMPT_KEYWORDS.split(',')
else:
SYSTEM_PROMPT_KEYWORDS = []
SYSTEM_PROMPT_MESSAGE = get_setting('SYSTEM_PROMPT_MESSAGE')
SYSTEM_PROMPT_GUIDE_MESSAGE = get_setting('SYSTEM_PROMPT_GUIDE_MESSAGE')
SYSTEM_PROMPT_QUICK_REPLY = get_setting('SYSTEM_PROMPT_QUICK_REPLY')
if SYSTEM_PROMPT_QUICK_REPLY:
SYSTEM_PROMPT_QUICK_REPLY = SYSTEM_PROMPT_QUICK_REPLY.split(',')
else:
SYSTEM_PROMPT_QUICK_REPLY = []
SYSTEM_PROMPT1_QUICK_REPLY = str(SYSTEM_PROMPT_QUICK_REPLY[0]) if len(SYSTEM_PROMPT_QUICK_REPLY) > 0 else []
SYSTEM_PROMPT2_QUICK_REPLY = str(SYSTEM_PROMPT_QUICK_REPLY[1]) if len(SYSTEM_PROMPT_QUICK_REPLY) > 1 else []
SYSTEM_PROMPT3_QUICK_REPLY = str(SYSTEM_PROMPT_QUICK_REPLY[2]) if len(SYSTEM_PROMPT_QUICK_REPLY) > 2 else []
SYSTEM_PROMPT4_QUICK_REPLY = str(SYSTEM_PROMPT_QUICK_REPLY[3]) if len(SYSTEM_PROMPT_QUICK_REPLY) > 3 else []
SYSTEM_PROMPT5_QUICK_REPLY = str(SYSTEM_PROMPT_QUICK_REPLY[4]) if len(SYSTEM_PROMPT_QUICK_REPLY) > 4 else []
def get_setting(key):
doc_ref = db.collection(u'settings').document('app_settings')
doc = doc_ref.get()
if doc.exists:
doc_dict = doc.to_dict()
if key not in doc_dict:
# If the key does not exist in the document, use the default value
default_value = DEFAULT_ENV_VARS.get(key, "")
doc_ref.set({key: default_value}, merge=True) # Add the new setting to the database
return default_value
else:
return doc_dict.get(key)
else:
# If the document does not exist, create it using the default settings
save_default_settings()
return DEFAULT_ENV_VARS.get(key, "")
def get_setting_user(user_id, key):
doc_ref = db.collection(u'users').document(user_id)
doc = doc_ref.get()
if doc.exists:
doc_dict = doc.to_dict()
if key not in doc_dict:
if key == 'start_free_day':
start_free_day = datetime.now(jst)
doc_ref.set({'start_free_day': start_free_day}, merge=True)
return ''
else:
return doc_dict.get(key)
else:
return ''
def save_default_settings():
doc_ref = db.collection(u'settings').document('app_settings')
doc_ref.set(DEFAULT_ENV_VARS, merge=True)
def update_setting(key, value):
doc_ref = db.collection(u'settings').document('app_settings')
doc_ref.update({key: value})
reload_settings()
app = Flask(__name__)
hash_object = SHA256.new(data=(secret_key or '').encode('utf-8'))
hashed_secret_key = hash_object.digest()
app.secret_key = os.getenv('secret_key', default='YOUR-DEFAULT-SECRET-KEY')
# Stripe webhook secret, used to verify the event
STRIPE_WEBHOOK_SECRET = os.getenv('STRIPE_WEBHOOK_SECRET')
@app.route('/reset_logs', methods=['POST'])
def reset_logs():
if 'is_admin' not in session or not session['is_admin']:
return redirect(url_for('login'))
else:
try:
users_ref = db.collection(u'users')
users = users_ref.stream()
for user in users:
user_ref = users_ref.document(user.id)
user_ref.delete()
return 'All user data reset successfully', 200
except Exception as e:
print(f"Error resetting user data: {e}")
return 'Error resetting user data', 500
@app.route('/login', methods=['GET', 'POST'])
def login():
attempts_doc_ref = db.collection(u'settings').document('admin_attempts')
attempts_doc = attempts_doc_ref.get()
attempts_info = attempts_doc.to_dict() if attempts_doc.exists else {}
attempts = attempts_info.get('attempts', 0)
lockout_time = attempts_info.get('lockout_time', None)
# ロックアウト状態をチェック
if lockout_time:
if datetime.now(jst) < lockout_time:
return render_template('login.html', message='Too many failed attempts. Please try again later.')
else:
# ロックアウト時間が過ぎたらリセット
attempts = 0
lockout_time = None
if request.method == 'POST':
password = request.form.get('password')
if password == admin_password:
session['is_admin'] = True
# ログイン成功したら試行回数とロックアウト時間をリセット
attempts_doc_ref.set({'attempts': 0, 'lockout_time': None})
return redirect(url_for('settings'))
else:
attempts += 1
lockout_time = datetime.now(jst) + timedelta(minutes=10) if attempts >= 5 else None
attempts_doc_ref.set({'attempts': attempts, 'lockout_time': lockout_time})
return render_template('login.html', message='Incorrect password. Please try again.')
return render_template('login.html')
@app.route('/settings', methods=['GET', 'POST'])
def settings():
if 'is_admin' not in session or not session['is_admin']:
return redirect(url_for('login'))
current_settings = {key: get_setting(key) or DEFAULT_ENV_VARS.get(key, '') for key in REQUIRED_ENV_VARS}
if request.method == 'POST':
for key in REQUIRED_ENV_VARS:
value = request.form.get(key)
if value:
update_setting(key, value)
return redirect(url_for('settings'))
return render_template(
'settings.html',
settings=current_settings,
default_settings=DEFAULT_ENV_VARS,
required_env_vars=REQUIRED_ENV_VARS
)
def get_encrypted_message(message, hashed_secret_key):
cipher = AES.new(hashed_secret_key, AES.MODE_ECB)
message = message.encode('utf-8')
padding = 16 - len(message) % 16
message += bytes([padding]) * padding
enc_message = base64.b64encode(cipher.encrypt(message))
return enc_message.decode()
def get_decrypted_message(enc_message, hashed_secret_key):
try:
cipher = AES.new(hashed_secret_key, AES.MODE_ECB)
enc_message = base64.b64decode(enc_message.encode('utf-8'))
message = cipher.decrypt(enc_message)
padding = message[-1]
if padding > 16:
raise ValueError("Invalid padding value")
message = message[:-padding]
return message.decode().rstrip("\0")
except Exception as e:
print(f"Error decrypting message: {e}")
return None
@firestore.transactional
def update_monthly_usage(transaction, doc_ref):
try:
snapshot = doc_ref.get(transaction=transaction)
monthly_usage = 0
monthly_free = False
if snapshot.exists:
last_updated = snapshot.get('last_updated')
last_updated_date = last_updated.astimezone(jst).date() if last_updated else None
monthly_usage = snapshot.get('monthly_usage') if snapshot.exists and 'monthly_usage' in snapshot.to_dict() else 0
monthly_free = snapshot.get('monthly_free') if 'monthly_free' in snapshot.to_dict() else False
if last_updated_date is None or last_updated_date.month != nowDate.month:
monthly_usage = 1 # 新しい月になったら1にリセット
monthly_free = False # 新しい月になったら無料期間をリセット
else:
monthly_usage += 1 # 同じ月ならインクリメント
# ドキュメントを更新
transaction.update(doc_ref, {
'monthly_usage': monthly_usage,
'monthly_free': monthly_free,
'last_updated': nowDate
})
else:
# ドキュメントが存在しない場合、新しいドキュメントを作成
monthly_usage = 1
transaction.set(doc_ref, {
'monthly_usage': monthly_usage,
'monthly_free': monthly_free,
'last_updated': nowDate
})
return monthly_usage, monthly_free
except Exception as e:
print(f"Error: {e}")
raise
@app.route("/", methods=["POST"])
def callback():
# get X-Line-Signature header value
signature = request.headers["X-Line-Signature"]
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
response = jsonify({'status': 'success'})
response.status_code = 200
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
print("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return "OK"
@handler.add(MessageEvent, message=(TextMessage, AudioMessage, LocationMessage, ImageMessage, StickerMessage, VideoMessage))
def handle_message(event):
reload_settings()
try:
user_id = event.source.user_id
profile = get_profile(user_id)
display_name = profile.display_name
reply_token = event.reply_token
message_type = event.message.type
message_id = event.message.id
source_type = event.source.type
start_loading_animation(user_id)
db = firestore.Client(database=DATABASE_NAME)
doc_ref = db.collection('settings').document('counter')
transaction = db.transaction()
monthly_usage, monthly_free = update_monthly_usage(transaction, doc_ref)
doc_ref = db.collection(u'users').document(user_id)
@firestore.transactional
def update_in_transaction(transaction, doc_ref):
user_message = []
exec_functions = False
quick_reply_items = []
head_message = ""
encoding: Encoding = tiktoken.encoding_for_model(GPT_MODEL)
messages = []
updated_date_string = nowDate
daily_usage = 0
start_free_day = datetime.now(jst)
audio_or_text = 'Text'
or_chinese = 'MANDARIN'
or_english = 'AMERICAN'
audio_speed = 'normal'
translate_language = 'OFF'
bot_name = BOT_NAME[0]
links = ""
bot_reply = ""
bot_reply_list = []
public_url = []
public_img_url = []
public_img_url_s = []
enable_quick_reply = False
gaccount_access_token = ""
gaccount_refresh_token = ""
free_duration = False
blocked_account = False
core_ai_type_personal = CORE_AI_TYPE
system_prompt_personal = 0
system_prompt_temp = SYSTEM_PROMPT1
if message_type == 'text':
user_message = event.message.text
elif message_type == 'audio':
user_message = get_audio(message_id)
elif message_type == 'sticker':
keywords = event.message.keywords
if keywords == "":
user_message = STICKER_FAIL_MESSAGE
else:
user_message = STICKER_MESSAGE + "\n" + ', '.join(keywords)
elif message_type =='image':
vision_results = vision_api(message_id, os.environ["CHANNEL_ACCESS_TOKEN"])
str_vision_results = str(vision_results)
str_vision_results = OCR_BOTGUIDE_MESSAGE + "\n" + str_vision_results
OCR_MESSAGE = get_setting('OCR_MESSAGE').format(display_name=display_name)
head_message = head_message + OCR_MESSAGE + "\n" + str_vision_results
user_message = OCR_USER_MESSAGE
elif message_type =='video':
videoitg_results = get_audio(message_id)
print(f"videoitg_results: {videoitg_results}")
str_videoitg_results = str(videoitg_results)
str_videoitg_results = VIDEO_BOTGUIDE_MESSAGE + "\n" + str_videoitg_results
VIDEO_MESSAGE = get_setting('VIDEO_MESSAGE').format(display_name=display_name)
head_message = head_message + VIDEO_MESSAGE + "\n" + str_videoitg_results
user_message = VIDEO_USER_MESSAGE
elif message_type == 'location':
latitude = event.message.latitude
longitude = event.message.longitude
result = get_addresses(latitude, longitude)
user_message = MAPS_MESSAGE + "\n" + result
doc = doc_ref.get(transaction=transaction)
if doc.exists:
user = doc.to_dict()
user['messages'] = [{**msg, 'content': get_decrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]
updated_date_string = user['updated_date_string']
daily_usage = user['daily_usage']
start_free_day = user['start_free_day']
audio_or_text = user['audio_or_text']
or_chinese = user['or_chinese']
or_english = user['or_english']
audio_speed = user['audio_speed']
translate_language = user['translate_language']
updated_date = user['updated_date_string'].astimezone(jst)
gaccount_access_token = user.get('gaccount_access_token', "")
gaccount_refresh_token = user.get('gaccount_refresh_token', "")
blocked_account = user.get('blocked_account', False)
core_ai_type_personal = user.get('core_ai_type_personal', CORE_AI_TYPE)
system_prompt_personal = user.get('system_prompt_personal', 0)
if nowDate.date() != updated_date.date():
daily_usage = 0
else:
daily_usage = daily_usage + 1
else:
if BLOCKED_NEW_ACCOUNTS == "True":
bot_reply_list.append(['text', BLOCKED_NEW_ACCOUNTS_MESSAGE])
line_reply(reply_token, bot_reply_list)
return 'OK'
user = {
'messages': messages,
'updated_date_string': nowDate,
'daily_usage': daily_usage,
'start_free_day': start_free_day,
'audio_or_text' : audio_or_text,
'or_chinese' : or_chinese,
'or_english' : or_english,
'audio_speed' : audio_speed,
'translate_language' : translate_language,
'core_ai_type_personal' : core_ai_type_personal,
'system_prompt_personal' : system_prompt_personal
}
transaction.set(doc_ref, user)
if user_message.strip() == FORGET_QUICK_REPLY:
bot_reply_list.append(['text', FORGET_MESSAGE])
line_reply(reply_token, bot_reply_list)
user['messages'] = []
transaction.set(doc_ref, user, merge=True)
return 'OK'
elif blocked_account == True:
bot_reply_list.append(['text', BLOCKED_ACCOUNT_MESSAGE])
line_reply(reply_token, bot_reply_list)
return 'OK'
elif CHANGE_TO_TEXT_QUICK_REPLY in user_message and (LINE_REPLY == "Audio" or LINE_REPLY == "VV"):
exec_functions == True
audio_or_text = "Text"
user['audio_or_text'] = audio_or_text
bot_reply_list.append(['text', CHANGE_TO_TEXT_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif CHANGE_TO_AUDIO_QUICK_REPLY in user_message and (LINE_REPLY == "Audio" or LINE_REPLY == "VV"):
exec_functions == True
audio_or_text = "Audio"
user['audio_or_text'] = audio_or_text
bot_reply_list.append(['text', CHANGE_TO_AUDIO_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif OR_CHINESE_MANDARIN_QUICK_REPLY in user_message and (LINE_REPLY == "Audio"):
exec_functions = True
or_chinese = "MANDARIN"
user['or_chinese'] = or_chinese
OR_CHINESE_MESSAGE = get_setting('OR_CHINESE_MESSAGE').format(or_chinese=or_chinese)
bot_reply_list.append(['text', OR_CHINESE_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif OR_CHINESE_CANTONESE_QUICK_REPLY in user_message and (LINE_REPLY == "Audio"):
exec_functions = True
or_chinese = "CANTONESE"
user['or_chinese'] = or_chinese
OR_CHINESE_MESSAGE = get_setting('OR_CHINESE_MESSAGE').format(or_chinese=or_chinese)
user_message = OR_CHINESE_MESSAGE
bot_reply_list.append(['text', OR_CHINESE_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif OR_ENGLISH_AMERICAN_QUICK_REPLY in user_message and (LINE_REPLY == "Audio"):
exec_functions = True
or_english = "AMERICAN"
user['or_english'] = or_english
OR_ENGLISH_MESSAGE = get_setting('OR_ENGLISH_MESSAGE').format(or_english=or_english)
bot_reply_list.append(['text', OR_ENGLISH_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif OR_ENGLISH_BRIDISH_QUICK_REPLY in user_message and (LINE_REPLY == "Audio"):
exec_functions = True
or_english = "BRIDISH"
user['or_english'] = or_english
OR_ENGLISH_MESSAGE = get_setting('OR_ENGLISH_MESSAGE').format(or_english=or_english)
bot_reply_list.append(['text', OR_ENGLISH_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif OR_ENGLISH_AUSTRALIAN_QUICK_REPLY in user_message and (LINE_REPLY == "Audio"):
exec_functions = True
or_english = "BRIDISH"
user['or_english'] = or_english
OR_ENGLISH_MESSAGE = get_setting('OR_ENGLISH_MESSAGE').format(or_english=or_english)
bot_reply_list.append(['text', OR_ENGLISH_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif OR_ENGLISH_INDIAN_QUICK_REPLY in user_message and (LINE_REPLY == "Audio"):
exec_functions = True
or_english = "INDIAN"
user['or_english'] = or_english
OR_ENGLISH_MESSAGE = get_setting('OR_ENGLISH_MESSAGE').format(or_english=or_english)
bot_reply_list.append(['text', OR_ENGLISH_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif AUDIO_SPEED_SLOW_QUICK_REPLY in user_message and (LINE_REPLY == "Audio"):
exec_functions = True
audio_speed = "slow"
user['audio_speed'] = audio_speed
AUDIO_SPEED_MESSAGE = get_setting('AUDIO_SPEED_MESSAGE').format(audio_speed=audio_speed)
bot_reply_list.append(['text', AUDIO_SPEED_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif AUDIO_SPEED_NORMAL_QUICK_REPLY in user_message and (LINE_REPLY == "Audio"):
exec_functions = True
audio_speed = "normal"
user['audio_speed'] = audio_speed
AUDIO_SPEED_MESSAGE = get_setting('AUDIO_SPEED_MESSAGE').format(audio_speed=audio_speed)
user_message = AUDIO_SPEED_MESSAGE
bot_reply_list.append(['text', AUDIO_SPEED_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif AUDIO_SPEED_FAST_QUICK_REPLY in user_message and (LINE_REPLY == "Audio"):
exec_functions = True
audio_speed = "fast"
user['audio_speed'] = audio_speed
AUDIO_SPEED_MESSAGE = get_setting('AUDIO_SPEED_MESSAGE').format(audio_speed=audio_speed)
bot_reply_list.append(['text', AUDIO_SPEED_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif TRANSLATE_OFF_QUICK_REPLY in user_message:
exec_functions = True
translate_language = "OFF"
user['translate_language'] = translate_language
TRANSLATE_OFF_MESSAGE = get_setting('TRANSLATE_OFF_MESSAGE').format(display_name=display_name)
bot_reply_list.append(['text', TRANSLATE_OFF_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif TRANSLATE_CHAINESE_QUICK_REPLY in user_message:
exec_functions = True
translate_language = "CHAINESE"
user['translate_language'] = translate_language
TRANSLATE_MESSAGE = get_setting('TRANSLATE_MESSAGE').format(translate_language=translate_language)
bot_reply_list.append(['text', TRANSLATE_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif TRANSLATE_ENGLISH_QUICK_REPLY in user_message:
exec_functions = True
translate_language = "ENGLISH"
user['translate_language'] = translate_language
TRANSLATE_MESSAGE = get_setting('TRANSLATE_MESSAGE').format(translate_language=translate_language)
bot_reply_list.append(['text', TRANSLATE_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif TRANSLATE_INDONESIAN_QUICK_REPLY in user_message:
exec_functions = True
translate_language = "INDONESIAN"
user['translate_language'] = translate_language
TRANSLATE_MESSAGE = get_setting('TRANSLATE_MESSAGE').format(translate_language=translate_language)
bot_reply_list.append(['text', TRANSLATE_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif TRANSLATE_JAPANESE_QUICK_REPLY in user_message:
exec_functions = True
translate_language = "JAPANESE"
user['translate_language'] = translate_language
TRANSLATE_MESSAGE = get_setting('TRANSLATE_MESSAGE').format(translate_language=translate_language)
bot_reply_list.append(['text', TRANSLATE_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif TRANSLATE_KOREAN_QUICK_REPLY in user_message:
exec_functions = True
translate_language = "KOREAN"
user['translate_language'] = translate_language
TRANSLATE_MESSAGE = get_setting('TRANSLATE_MESSAGE').format(translate_language=translate_language)
bot_reply_list.append(['text', TRANSLATE_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif TRANSLATE_THAIAN_QUICK_REPLY in user_message:
exec_functions = True
translate_language = "THAI"
user['translate_language'] = translate_language
TRANSLATE_MESSAGE = get_setting('TRANSLATE_MESSAGE').format(translate_language=translate_language)
bot_reply_list.append(['text', TRANSLATE_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif CORE_AI_TYPE_GPT_QUICK_REPLY in user_message:
exec_functions = True
user['core_ai_type_personal'] = "GPT"
CORE_AI_TYPE_MESSAGE = get_setting('CORE_AI_TYPE_MESSAGE')
bot_reply_list.append(['text', CORE_AI_TYPE_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif CORE_AI_TYPE_GEMINI_QUICK_REPLY in user_message:
exec_functions = True
user['core_ai_type_personal'] = "Vertex"
CORE_AI_TYPE_MESSAGE = get_setting('CORE_AI_TYPE_MESSAGE')
bot_reply_list.append(['text', CORE_AI_TYPE_MESSAGE])
line_reply(reply_token, bot_reply_list)
transaction.set(doc_ref, {**user, 'messages': [{**msg, 'content': get_encrypted_message(msg['content'], hashed_secret_key)} for msg in user['messages']]})
return 'OK'
elif CORE_AI_TYPE_CLAUDE_QUICK_REPLY in user_message:
exec_functions = True
user['core_ai_type_personal'] = "Claude"
CORE_AI_TYPE_MESSAGE = get_setting('CORE_AI_TYPE_MESSAGE')
bot_reply_list.append(['text', CORE_AI_TYPE_MESSAGE])