18
18
from . import db , speech
19
19
from .whitelisthandler import WhitelistHandler
20
20
from channel import EFBChannel , EFBMsg , MsgType , MsgSource , TargetType , ChannelType
21
- from channelExceptions import EFBChatNotFound , EFBMessageTypeNotSupported
21
+ from channelExceptions import EFBChatNotFound , EFBMessageTypeNotSupported , EFBMessageError
22
22
from .msgType import get_msg_type , TGMsgType
23
23
from moviepy .editor import VideoFileClip
24
24
@@ -65,6 +65,9 @@ class TelegramChannel(EFBChannel):
65
65
channel_emoji = "✈"
66
66
channel_id = "eh_telegram_master"
67
67
channel_type = ChannelType .Master
68
+ supported_message_types = {MsgType .Text , MsgType .File , MsgType .Audio ,
69
+ MsgType .Command , MsgType .Image , MsgType .Link , MsgType .Location ,
70
+ MsgType .Sticker , MsgType .Video }
68
71
69
72
# Data
70
73
slaves = None
@@ -73,6 +76,19 @@ class TelegramChannel(EFBChannel):
73
76
msg_storage = {}
74
77
me = None
75
78
79
+ # Constants
80
+ TYPE_DICT = {
81
+ TGMsgType .Text : MsgType .Text ,
82
+ TGMsgType .Audio : MsgType .Audio ,
83
+ TGMsgType .Document : MsgType .File ,
84
+ TGMsgType .Photo : MsgType .Image ,
85
+ TGMsgType .Sticker : MsgType .Sticker ,
86
+ TGMsgType .Video : MsgType .Video ,
87
+ TGMsgType .Voice : MsgType .Audio ,
88
+ TGMsgType .Location : MsgType .Location ,
89
+ TGMsgType .Venue : MsgType .Location ,
90
+ }
91
+
76
92
def __init__ (self , queue , slaves ):
77
93
"""
78
94
Initialization.
@@ -269,7 +285,7 @@ def process_msg(self, msg):
269
285
os .remove (msg .path )
270
286
return self .bot .bot .sendMessage (tg_dest , msg_template % ("Error: Empty %s received. (MS01)" % msg .type ))
271
287
if not msg .text :
272
- if MsgType .Image :
288
+ if msg . type == MsgType .Image :
273
289
msg .text = "sent a picture."
274
290
elif msg .type == MsgType .Sticker :
275
291
msg .text = "sent a sticker."
@@ -342,7 +358,8 @@ def process_msg(self, msg):
342
358
"slave_origin_uid" : "%s.%s" % (msg .channel_id , msg .origin ['uid' ]),
343
359
"slave_origin_display_name" : msg .origin ['alias' ],
344
360
"slave_member_uid" : msg .member ['uid' ],
345
- "slave_member_display_name" : msg .member ['alias' ]}
361
+ "slave_member_display_name" : msg .member ['alias' ],
362
+ "slave_message_uid" : msg .uid }
346
363
if tg_chat_assoced and append_last_msg :
347
364
msg_log ['update' ] = True
348
365
db .add_msg_log (** msg_log )
@@ -801,6 +818,14 @@ def msg(self, bot, update):
801
818
# Type specific stuff
802
819
self .logger .debug ("Msg type: %s" , mtype )
803
820
821
+ if self .TYPE_DICT .get (mtype , None ):
822
+ m .type = self .TYPE_DICT [mtype ]
823
+ else :
824
+ raise EFBMessageTypeNotSupported ()
825
+
826
+ if m .type not in self .slaves [channel ].supported_message_types :
827
+ raise EFBMessageTypeNotSupported ()
828
+
804
829
if mtype == TGMsgType .Text :
805
830
m .type = MsgType .Text
806
831
m .text = update .message .text
@@ -812,12 +837,12 @@ def msg(self, bot, update):
812
837
m .file = open (m .path , "rb" )
813
838
elif mtype == TGMsgType .Sticker :
814
839
m .type = MsgType .Sticker
815
- m .text = update . message . sticker . emoji
840
+ m .text = ""
816
841
tg_file_id = update .message .sticker .file_id
817
842
m .path , m .mime = self ._download_file (update .message , tg_file_id , m .type )
818
843
m .file = open (m .path , "rb" )
819
844
elif mtype == TGMsgType .Document :
820
- m .text = update .message .document .file_name
845
+ m .text = update .message .document .file_name + " \n " + update . message . caption
821
846
tg_file_id = update .message .document .file_id
822
847
self .logger .debug ("tg: Document file received" )
823
848
if update .message .document .mime_type == "video/mp4" :
@@ -830,18 +855,18 @@ def msg(self, bot, update):
830
855
m .file = open (m .path , "rb" )
831
856
elif mtype == TGMsgType .Video :
832
857
m .type = MsgType .Video
833
- m .text = update .message .document . file_name
858
+ m .text = update .message .caption
834
859
tg_file_id = update .message .document .file_id
835
860
m .path , m .mime = self ._download_file (update .message , tg_file_id , m .type )
836
861
m .file = open (m .path , "rb" )
837
862
elif mtype == TGMsgType .Audio :
838
863
m .type = MsgType .Audio
839
- m .text = "%s - %s" % (update .message .audio .title , update .message .audio .perfomer )
864
+ m .text = "%s - %s\n %s " % (update .message .audio .title , update .message .audio .perfomer , update . message . caption )
840
865
tg_file_id = update .message .audio .file_id
841
866
m .path , m .mime = self ._download_file (update .message , tg_file_id , m .type )
842
867
elif mtype == TGMsgType .Voice :
843
868
m .type = MsgType .Audio
844
- m .text = ""
869
+ m .text = update . message . caption
845
870
tg_file_id = update .message .voice .file_id
846
871
m .path , m .mime = self ._download_file (update .message , tg_file_id , m .type )
847
872
elif mtype == TGMsgType .Location :
@@ -866,6 +891,8 @@ def msg(self, bot, update):
866
891
return self ._reply_error (bot , update , "Internal error: Chat not found in channel. (CN01)" )
867
892
except EFBMessageTypeNotSupported :
868
893
return self ._reply_error (bot , update , "Message type not supported. (MN01)" )
894
+ except EFBMessageError as e :
895
+ return self ._reply_error (bot , update , "Message is not sent. (MN01)\n \n %s" % str (e ))
869
896
870
897
def _download_file (self , tg_msg , file_id , msg_type ):
871
898
"""
0 commit comments