2019-04-06 06:12:54 +00:00
class TelegramClient
2019-05-04 20:17:29 +00:00
attr_reader :jid , :login , :online , :auth_state , :me
attr_accessor :timezone
2019-04-06 06:12:54 +00:00
# tdlib configuration, shared within all instances #
def self . configure ( params )
2019-04-07 11:58:07 +00:00
@@loglevel = params [ 'loglevel' ] || Logger :: DEBUG
@@content_path = params [ 'content_path' ] || '/tmp'
@@content_link = params [ 'content_link' ] || 'https://localhost/tg_media'
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
@@content_upload_prefix = params [ " content_upload_prefix " ] || 'https://localhost/upload/'
2019-04-06 06:12:54 +00:00
TD . configure do | config |
2019-04-07 11:58:07 +00:00
config . lib_path = params [ 'path' ] || 'lib/' # we hope it's here
2019-04-13 14:42:56 +00:00
config . client . api_id = params [ 'api_id' ] || '50322' # telegram app. from debian repositories
config . client . api_hash = params [ 'api_hash' ] || '9ff1a639196c0779c86dd661af8522ba' # telegram app. from debian repositories
config . client . device_model = params [ 'useragent' ] || 'Zhabogram'
config . client . application_version = params [ 'version' ] || '1.0' # hmm...
2019-04-07 11:58:07 +00:00
config . client . use_test_dc = params [ 'use_test_dc' ] || false
config . client . system_version = '42' # I think I have permission to hardcode The Ultimate Question of Life, the Universe, and Everything?..
2019-05-07 19:22:26 +00:00
config . client . use_file_database = false # wow
config . client . use_message_database = false # such library
2019-04-13 14:42:56 +00:00
config . client . use_chat_info_database = false # much options
2019-05-07 19:22:26 +00:00
config . client . enable_storage_optimizer = false # ...
2019-04-06 06:12:54 +00:00
end
2019-04-07 11:58:07 +00:00
TD :: Api . set_log_verbosity_level ( params [ 'verbosity' ] || 1 )
2019-04-06 06:12:54 +00:00
end
# instance initialization #
2019-05-04 20:17:29 +00:00
def initialize ( xmpp , jid , login )
2019-05-04 22:42:59 +00:00
return if not @@loglevel # call .configure() first
2019-05-04 20:17:29 +00:00
@logger = Logger . new ( STDOUT ) ; @logger . level = @@loglevel ; @logger . progname = '[TelegramClient: %s/%s]' % [ jid , login ] # create logger
2019-05-04 22:42:59 +00:00
@logger . info 'Starting Telegram client..'
2019-05-04 20:17:29 +00:00
@xmpp = xmpp # XMPP stream
@jid = jid # user JID
@timezone = '-00:00' # default timezone is UTC
@login = login # telegram login
@me = nil # self telegram profile
@online = nil # we do not know
@auth_state = 'nil' # too.
2019-05-07 19:22:26 +00:00
@cache = { chats : { } , users : { } , users_fullinfo : { } , userpics : { } , unread_msg : { } , incoming_msg : [ ] } # cache storage
2019-05-04 20:17:29 +00:00
@files_dir = File . dirname ( __FILE__ ) + '/../sessions/' + @jid + '/files/'
2019-05-04 22:42:59 +00:00
end
# initialize and connect telegram client #
def connect ( )
return if @client and @client . ready?
@logger . info 'Connecting to Telegram network..'
2019-05-04 20:17:29 +00:00
@client = TD :: Client . new ( database_directory : 'sessions/' + @jid , files_directory : 'sessions/' + @jid + '/files/' ) # create telegram client instance
2019-04-06 06:12:54 +00:00
@client . on ( TD :: Types :: Update :: AuthorizationState ) do | update | self . auth_handler ( update ) end # register auth update handler
@client . on ( TD :: Types :: Update :: NewMessage ) do | update | self . message_handler ( update ) end # register new message update handler
2019-04-07 11:58:07 +00:00
@client . on ( TD :: Types :: Update :: MessageContent ) do | update | self . message_edited_handler ( update ) end # register msg edited handler
@client . on ( TD :: Types :: Update :: DeleteMessages ) do | update | self . message_deleted_handler ( update ) end # register msg del handler
@client . on ( TD :: Types :: Update :: File ) do | update | self . file_handler ( update ) end # register file handler
@client . on ( TD :: Types :: Update :: NewChat ) do | update | self . new_chat_handler ( update ) end # register new chat handler
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
@client . on ( TD :: Types :: Update :: User ) do | update | self . user_handler ( update ) end # new user update?
2019-04-07 11:58:07 +00:00
@client . on ( TD :: Types :: Update :: UserStatus ) do | update | self . status_update_handler ( update ) end # register status handler
2019-05-04 22:42:59 +00:00
@client . connect ( )
2019-05-04 20:17:29 +00:00
end
2019-05-04 22:42:59 +00:00
# disconnect and destroy telegram client #
2019-05-04 20:17:29 +00:00
def disconnect ( logout = false )
2019-05-04 22:42:59 +00:00
return if not @client
2019-05-04 20:17:29 +00:00
@logger . info 'Disconnecting..'
@cache [ :chats ] . each_key do | chat_id | @xmpp . presence ( @jid , chat_id . to_s , :unavailable ) end # send offline presences
( logout ) ? @client . log_out : @client . dispose # logout if needed
@online = false
2019-05-07 19:22:26 +00:00
@xmpp . presence ( @jid , nil , :unavailable )
2019-05-04 20:17:29 +00:00
end
2019-04-06 07:29:48 +00:00
###########################################
## Callback handlers #####################
###########################################
2019-04-06 06:12:54 +00:00
# authorization handler #
def auth_handler ( update )
2019-04-06 07:29:48 +00:00
@logger . debug 'Authorization state changed: %s' % update . authorization_state
2019-05-04 20:17:29 +00:00
@auth_state = update . authorization_state . class . name
2019-04-06 06:12:54 +00:00
2019-04-06 07:29:48 +00:00
case update . authorization_state
2019-04-06 06:12:54 +00:00
# auth stage 0: specify login #
when TD :: Types :: AuthorizationState :: WaitPhoneNumber
2019-04-06 07:29:48 +00:00
@logger . info 'Logging in..'
2019-04-06 06:12:54 +00:00
@client . set_authentication_phone_number ( @login )
# auth stage 1: wait for authorization code #
when TD :: Types :: AuthorizationState :: WaitCode
2019-04-06 07:29:48 +00:00
@logger . info 'Waiting for authorization code..'
2019-05-04 20:17:29 +00:00
@xmpp . message ( @jid , nil , 'Please, enter authorization code via /code 12345' )
2019-04-06 06:12:54 +00:00
# auth stage 2: wait for 2fa passphrase #
when TD :: Types :: AuthorizationState :: WaitPassword
2019-04-06 07:29:48 +00:00
@logger . info 'Waiting for 2FA password..'
2019-05-04 20:17:29 +00:00
@xmpp . message ( @jid , nil , 'Please, enter 2FA passphrase via /password 12345' )
2019-04-07 11:58:07 +00:00
# authorization successful -- indicate that client is online and retrieve contact list #
2019-04-06 06:12:54 +00:00
when TD :: Types :: AuthorizationState :: Ready
2019-04-06 07:29:48 +00:00
@logger . info 'Authorization successful!'
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
@client . get_me ( ) . then { | user | @me = user } . wait
2019-04-13 14:42:56 +00:00
@client . get_chats ( limit = 9999 )
2019-04-07 11:58:07 +00:00
@logger . info " Contact list updating finished "
2019-05-04 20:17:29 +00:00
@xmpp . presence ( @jid , nil , :subscribe )
2019-05-07 19:22:26 +00:00
@xmpp . presence ( @jid , nil , nil , nil , " Logged in as %s " % @login )
@online = true
2019-04-13 14:42:56 +00:00
# closing session: sent offline presences to XMPP user #
when TD :: Types :: AuthorizationState :: Closing
@logger . info 'Closing session..'
self . disconnect ( )
# session closed gracefully
2019-04-07 11:58:07 +00:00
when TD :: Types :: AuthorizationState :: Closed
@logger . info 'Session closed.'
2019-04-13 14:42:56 +00:00
self . disconnect ( )
2019-04-06 06:12:54 +00:00
end
end
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
2019-04-06 06:12:54 +00:00
# message from telegram network handler #
2019-04-14 14:41:40 +00:00
def message_handler ( update , show_date = false )
2019-04-09 06:42:42 +00:00
@logger . debug 'Got NewMessage update'
2019-04-07 11:58:07 +00:00
@logger . debug update . message . to_json
2019-05-07 19:22:26 +00:00
2019-04-16 03:45:56 +00:00
@logger . info 'New message from Telegram chat %s' % update . message . chat_id
2019-05-07 19:22:26 +00:00
# we need to ignore incoming messages sometime
@cache [ :incoming_msg ] . shift ( 900 ) if @cache [ :incoming_msg ] . size > 1000 # clean cache if it exceeds 1000 messages (but remain last 100 entires)
return if @cache [ :incoming_msg ] . include? update . message . id # ignore message if it's already seen
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
return if update . message . is_outgoing and update . message . sending_state . instance_of? TD :: Types :: MessageSendingState :: Pending # ignore self outgoing messages
2019-05-07 19:22:26 +00:00
2019-04-07 11:58:07 +00:00
# media? #
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
file = nil
2019-04-14 14:41:40 +00:00
prefix = ''
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
text = ''
case update . message . content
when TD :: Types :: MessageContent :: Photo # photos
file = update . message . content . photo . sizes [ - 1 ] . photo
text = " %s.jpg (image/jpeg), %d bytes | %s | %s " % [ update . message . content . photo . id . to_s , file . size . to_i , self . format_content_link ( file . remote . id , 'image.jpg' ) , update . message . content . caption . text . to_s ]
when TD :: Types :: MessageContent :: Animation # "gif" (mp4) animations
file = update . message . content . animation . animation
text = " gif: %s | %s " % [ update . message . content . animation . file_name , self . format_content_link ( file . remote . id , 'video.mp4' ) ]
when TD :: Types :: MessageContent :: Sticker # stickers
file = update . message . content . sticker . sticker
text = " sticker: %s | %s " % [ update . message . content . sticker . emoji . to_s , self . format_content_link ( file . remote . id , 'sticker.webp' ) ]
when TD :: Types :: MessageContent :: Audio # music files
file = update . message . content . audio . audio
text = " %s (%s), %d bytes | %s | %s " % [ update . message . content . audio . file_name . to_s , update . message . content . audio . mime_type . to_s , file . size . to_i , self . format_content_link ( file . remote . id , update . message . content . audio . file_name . to_s ) , update . message . content . caption . text . to_s ]
when TD :: Types :: MessageContent :: Video # video files
file = update . message . content . video . video
text = " %s (%s), %d bytes | %s | %s " % [ update . message . content . video . file_name . to_s , update . message . content . video . mime_type . to_s , file . size . to_i , self . format_content_link ( file . remote . id , update . message . content . video . file_name . to_s ) , update . message . content . caption . text . to_s ]
when TD :: Types :: MessageContent :: VoiceNote # voice messages
file = update . message . content . voice_note . voice
text = " voice message (%i s.) | %s " % [ update . message . content . voice_note . duration , self . format_content_link ( file . remote . id , 'voice.oga' ) ]
when TD :: Types :: MessageContent :: Document # documents
file = update . message . content . document . document
text = " %s (%s), %d bytes | %s | %s " % [ update . message . content . document . file_name . to_s , update . message . content . document . mime_type . to_s , file . size . to_i , self . format_content_link ( file . remote . id , update . message . content . document . file_name . to_s ) , update . message . content . caption . text . to_s ]
2019-05-07 19:22:26 +00:00
when TD :: Types :: MessageContent :: BasicGroupChatCreate , TD :: Types :: MessageContent :: SupergroupChatCreate # group created
text = " created "
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
when TD :: Types :: MessageContent :: ChatJoinByLink # joined member
text = " joined "
when TD :: Types :: MessageContent :: ChatAddMembers # add members
text = " added "
update . message . content . member_user_ids . each do | member | text = text + self . format_username ( member ) + ' ' end
when TD :: Types :: MessageContent :: ChatDeleteMember # kicked member
text = " removed %s " % self . format_username ( update . message . content . user_id )
when TD :: Types :: MessageContent :: PinMessage # pinned message
@client . get_message ( update . message . chat_id , update . message . content . message_id ) . then { | message | text = " pinned message: %s " % message . content . text . text . to_s } . wait
when TD :: Types :: MessageContent :: ChatChangeTitle # changed chat title
text = " chat title set to: %s " % update . message . content . title . to_s
when TD :: Types :: MessageContent :: Location # location
location = " %s,%s " % [ update . message . content . location . latitude . to_s , update . message . content . location . longitude . to_s ]
text = " coordinates: %s | https://www.google.com/maps/search/%s/ " % [ location , location ]
when TD :: Types :: MessageContent :: Text # plain text
text = update . message . content . text . text . to_s
else
text = " unknown message type %s " % update . message . content . class
2019-04-07 11:58:07 +00:00
end
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
@client . download_file ( file . id ) if file # download it if already not
2019-04-07 11:58:07 +00:00
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
# forwards, replies and message id..
2019-05-04 20:17:29 +00:00
prefix += " [%s] " % DateTime . strptime ( ( update . message . date + Time . now . getlocal ( @timezone ) . utc_offset ) . to_s , '%s' ) . strftime ( " [%d %b %Y %H:%M:%S] " ) if show_date
2019-04-14 14:41:40 +00:00
prefix += " fwd from %s | " % self . format_username ( update . message . forward_info . sender_user_id ) if update . message . forward_info . instance_of? TD :: Types :: MessageForwardInfo :: MessageForwardedFromUser # fwd from user
prefix += " fwd from %s | " % self . format_chatname ( update . message . forward_info . chat_id ) if update . message . forward_info . instance_of? TD :: Types :: MessageForwardInfo :: MessageForwardedPost # fwd from chat
prefix += " reply to %s | " % self . format_reply ( update . message . chat_id , update . message . reply_to_message_id ) if update . message . reply_to_message_id . to_i != 0 # reply to
# text formatting
text = " %s | %s | %s \n %s " % [ update . message . id , self . format_username ( update . message . sender_user_id ) , prefix , text ] if update . message . chat_id < 0 # groupchats
2019-04-16 04:53:31 +00:00
text = " %s %s | %s%s " % [ ( update . message . is_outgoing ? '🠚' : '🠘' ) , update . message . id . to_s , prefix , text ] if update . message . chat_id > 0 # private chats
2019-04-14 02:56:29 +00:00
2019-04-08 22:28:26 +00:00
# send and add message id to unreads
@cache [ :unread_msg ] [ update . message . chat_id ] = update . message . id
2019-05-07 19:22:26 +00:00
@cache [ :incoming_msg ] << update . message . id
2019-05-04 20:17:29 +00:00
@xmpp . message ( @jid , update . message . chat_id . to_s , text )
2019-04-07 11:58:07 +00:00
end
# new chat update -- when tg client discovers new chat #
def new_chat_handler ( update )
2019-04-09 06:42:42 +00:00
@logger . debug 'Got NewChat update'
2019-04-07 11:58:07 +00:00
@logger . debug update . to_json
self . process_chat_info ( update . chat . id )
end
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
# user -- something changed in user data #
def user_handler ( update )
@logger . debug 'Got User update'
@logger . debug update . to_json
self . process_user_info ( update . user . id )
end
2019-04-07 11:58:07 +00:00
# edited msg #
def message_edited_handler ( update )
2019-04-09 06:42:42 +00:00
@logger . debug 'Got MessageEdited update'
2019-04-07 11:58:07 +00:00
@logger . debug update . to_json
# formatting
2019-04-16 03:45:56 +00:00
text = " ✎ %s | %s " % [ update . message_id . to_s , update . new_content . text . text . to_s ]
2019-05-04 20:17:29 +00:00
@xmpp . message ( @jid , update . chat_id . to_s , text )
2019-04-06 06:12:54 +00:00
end
2019-04-07 11:58:07 +00:00
# deleted msg #
def message_deleted_handler ( update )
2019-04-09 06:42:42 +00:00
@logger . debug 'Got MessageDeleted update'
2019-04-07 11:58:07 +00:00
@logger . debug update . to_json
return if not update . is_permanent
2019-04-16 03:45:56 +00:00
text = " ✗ %s | " % update . message_ids . join ( ',' )
2019-05-04 20:17:29 +00:00
@xmpp . message ( @jid , update . chat_id . to_s , text )
2019-04-07 11:58:07 +00:00
end
# file msg -- symlink to download path #
def file_handler ( update )
2019-04-09 06:42:42 +00:00
@logger . debug 'Got File update'
2019-04-07 11:58:07 +00:00
@logger . debug update . to_json
if update . file . local . is_downloading_completed then
fname = update . file . local . path . to_s
2019-04-13 14:42:56 +00:00
target = " %s/%s%s " % [ @@content_path , Digest :: SHA256 . hexdigest ( update . file . remote . id ) , File . extname ( fname ) ]
2019-04-07 11:58:07 +00:00
@logger . debug 'Downloading of <%s> completed! Link to <%s>' % [ fname , target ]
File . symlink ( fname , target )
end
end
# status update handler #
def status_update_handler ( update )
2019-04-09 06:42:42 +00:00
@logger . debug 'Got new StatusUpdate'
2019-04-07 11:58:07 +00:00
@logger . debug update . to_json
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
return if update . user_id == @me . id # ignore self statuses
2019-05-07 19:22:26 +00:00
self . process_status_update ( update . user_id , update . status , false )
2019-04-07 11:58:07 +00:00
end
2019-04-06 06:12:54 +00:00
2019-04-06 07:29:48 +00:00
###########################################
## LooP handlers #########################
###########################################
2019-04-06 06:12:54 +00:00
# processing authorization #
2019-04-13 14:42:56 +00:00
def process_auth ( typ , auth_data )
2019-04-16 03:45:56 +00:00
@logger . info " Authorizing with :%s.. " % typ
2019-04-13 14:42:56 +00:00
@client . check_authentication_code ( auth_data ) if typ == '/code'
@client . check_authentication_password ( auth_data ) if typ == '/password'
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
end
# /command #
def process_command ( chat_id , text )
splitted = text . split # splitted[0] = command, splitted[1] = argument
splitted = [ '/sed' , text [ 3 .. - 1 ] ] if text [ 0 .. 2 ] == '/s/' # sed-like edit
resolved = nil ; response = nil
# if second argument starts with @, try to resolve it
@client . search_public_chat ( splitted [ 1 ] [ 1 .. - 1 ] ) . then { | chat | resolved = chat } . wait if splitted [ 1 ] and splitted [ 1 ] [ 0 ] == '@'
case splitted [ 0 ]
2019-04-16 14:07:15 +00:00
when '/info' # information about user / chat
response = ''
2019-05-07 19:22:26 +00:00
id = ( resolved ) ? resolved . id : splitted [ 1 ]
id || = chat_id
id = id . to_i
2019-04-16 14:07:15 +00:00
self . process_user_info ( id ) if id and id > 0 and not @cache [ :users ] . key? id
self . process_chat_info ( id , false ) if id and id < 0 and not @cache [ :cache ] . key? id
response = self . format_chatname ( id ) if @cache [ :chats ] . key? id
response = self . format_username ( id , true ) if @cache [ :users ] . key? id
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
when '/add' # open new private chat by its id
chat = ( resolved ) ? resolved . id : splitted [ 1 ] . to_i
2019-04-13 15:39:20 +00:00
self . process_chat_info ( chat ) if chat != 0
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
when '/join' # join group/supergroup by invite link or by id
2019-04-13 15:39:20 +00:00
chat = ( resolved ) ? resolved . id : splitted [ 1 ]
2019-05-07 19:22:26 +00:00
chat || = chat_id
2019-04-13 15:39:20 +00:00
chat . to_s [ 0 .. 3 ] == " http " ? @client . join_chat_by_invite_link ( chat ) . wait : @client . join_chat ( chat . to_i ) . wait
2019-04-14 14:41:40 +00:00
when '/secret' # create new secret chat
2019-05-07 19:22:26 +00:00
uid = ( resolved ) ? resolved . id : chat_id
@client . create_new_secret_chat ( uid ) if uid > 0
2019-04-14 14:41:40 +00:00
when '/group' # create new group with @user_id
@client . create_new_basic_group_chat ( [ resolved . id ] , splitted [ 2 ] ) if resolved and splitted [ 2 ]
when '/supergroup' # create new supergroup
@client . create_new_supergroup_chat ( splitted [ 1 ] , splitted [ 2 ] ) if splitted [ 2 ]
when '/channel' # create new channel
@client . create_new_supergroup_chat ( splitted [ 1 ] , splitted [ 2 ] , is_channel : true ) if splitted [ 2 ]
2019-04-16 13:24:03 +00:00
when '/members' # view members of a group
2019-04-16 14:07:15 +00:00
response = " - Members of chat %s - \n \n " % @cache [ :chats ] [ chat_id ] . title
2019-04-16 13:24:03 +00:00
# supergroup
if @cache [ :chats ] [ chat_id ] . type . instance_of? TD :: Types :: ChatType :: Supergroup then
@client . get_supergroup_members ( @cache [ :chats ] [ chat_id ] . type . supergroup_id , TD :: Types :: SupergroupMembersFilter :: Recent . new ( ) , 0 , 200 ) . then { | members | members . members . each do | member |
2019-04-16 14:07:15 +00:00
self . process_user_info ( member . user_id ) if not @cache [ :users ] . key? member . user_id # fetch userdata if noinfo
2019-04-16 13:24:03 +00:00
response += ( @cache [ :users ] . key? member . user_id ) ? self . format_username ( member . user_id , true ) : " ID %s " % member . user_id
response += " | %s \n " % member . status . class . to_s
end } . wait
# normal group
elsif @cache [ :chats ] [ chat_id ] . type . instance_of? TD :: Types :: ChatType :: BasicGroup then
@cache [ :chats ] [ chat_id ] . last_message . content . member_user_ids . each do | member | response += ( @cache [ :users ] . key? member ) ? self . format_username ( member , true ) : " ID %s " % member ; response += " \n " end
end
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
when '/invite' # invite user to chat
2019-04-13 15:39:20 +00:00
@client . add_chat_member ( chat_id , resolved . id ) . wait if resolved
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
when '/kick' # removes user from chat
2019-04-13 15:39:20 +00:00
@client . set_chat_member_status ( chat_id , resolved . id , TD :: Types :: ChatMemberStatus :: Left . new ( ) ) . wait if resolved
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
when '/ban' # removes user from chat. argument = hours to ban.
until_date = ( splitted [ 1 ] ) ? Time . now . getutc . to_i + splitted [ 1 ] . to_i * 3600 : 0
2019-04-13 15:39:20 +00:00
@client . set_chat_member_status ( chat_id , resolved . id , TD :: Types :: ChatMemberStatus :: Banned . new ( banned_until_date : until_date ) ) . wait if resolved
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
when '/block' # add user to blacklist
@client . block_user ( chat_id )
when '/unblock' # add user to blacklist
@client . unblock_user ( chat_id )
when '/leave' , '/delete' # delete / leave chat
@client . close_chat ( chat_id ) . wait
@client . leave_chat ( chat_id ) . wait
2019-05-07 19:22:26 +00:00
@client . close_secret_chat ( @cache [ :chats ] [ chat_id ] . type . secret_chat_id ) . wait if @cache [ :chats ] [ chat_id ] . type . instance_of? TD :: Types :: ChatType :: Secret
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
@client . delete_chat_history ( chat_id , true ) . wait
2019-05-04 20:17:29 +00:00
@xmpp . presence ( @jid , chat_id , :unsubscribed )
@xmpp . presence ( @jid , chat_id , :unavailable )
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
@cache [ :chats ] . delete ( chat_id )
when '/sed' # sed-like edit
sed = splitted [ 1 ] . split ( '/' )
@client . search_chat_messages ( chat_id , 0 , 1 , sender_user_id : @me . id , filter : TD :: Types :: SearchMessagesFilter :: Empty . new ) . then { | msgs |
original = msgs . messages [ 0 ] . content . text . text . to_s
edited = ( sed [ 0 ] != '' ) ? original . gsub ( Regexp . new ( sed [ 0 ] ) , sed [ 1 ] ) : sed [ 1 ]
@client . edit_message_text ( chat_id , msgs . messages [ 0 ] . id , TD :: Types :: InputMessageContent :: Text . new ( :text = > { :text = > edited , :entities = > [ ] } , :disable_web_page_preview = > false , :clear_draft = > true ) ) if edited != original
} . wait
when '/d' # delete last message
2019-04-14 14:41:40 +00:00
id = splitted [ 1 ] . to_i
@client . search_chat_messages ( chat_id , 0 , 1 , sender_user_id : @me . id , filter : TD :: Types :: SearchMessagesFilter :: Empty . new ) . then { | msgs | id = msgs . messages [ 0 ] . id } . wait if not id or id == 0
@client . delete_messages ( chat_id , [ id ] , true )
2019-04-14 02:56:29 +00:00
when '/dump'
response = @cache [ :chats ] [ chat_id ] . to_json
2019-04-14 14:41:40 +00:00
when '/search'
count = ( splitted [ 1 ] ) ? splitted [ 1 ] . to_i : 10
query = ( splitted [ 2 ] ) ? splitted [ 2 ] : nil
@client . search_chat_messages ( chat_id , 0 , count , query : query , filter : TD :: Types :: SearchMessagesFilter :: Empty . new ) . then { | msgs |
msgs . messages . reverse . each do | msg | self . message_handler ( TD :: Types :: Update :: NewMessage . new ( message : msg , disable_notification : false , contains_mention : false ) , true ) end
} . wait
when '/history'
count = ( splitted [ 1 ] ) ? splitted [ 1 ] . to_i : 10
@client . get_chat_history ( chat_id , 0 , 0 , count ) . then { | msgs |
msgs . messages . reverse . each do | msg | self . message_handler ( TD :: Types :: Update :: NewMessage . new ( message : msg , disable_notification : false , contains_mention : false ) , true ) end
} . wait
2019-04-16 04:00:09 +00:00
when '/setusername'
@client . set_username ( splitted [ 1 ] ) if splitted [ 1 ]
when '/setname'
@client . set_name ( splitted [ 1 ] , splitted [ 2 ] ) if splitted [ 1 ]
when '/setbio'
@client . set_bio ( splitted [ 1 ] ) if splitted [ 1 ]
when '/setpassword'
old_password = splitted [ 1 ]
new_password = splitted [ 2 ]
old_password = '' if old_password == 'nil'
new_password = nil if new_password == 'nil'
@client . set_password ( old_password , new_password : new_password )
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
else
response = ' Unknown command .
/ s /mi tsake / mistake / — Edit last message
/ d — Delete last message
2019-04-16 14:07:15 +00:00
/ info id — Information about user / chat by its id
2019-04-14 14:41:40 +00:00
/ add @username or id — Create conversation with specified user or chat id
2019-04-16 14:07:15 +00:00
/ join chat_link or id — Join chat by its link or id
2019-04-14 14:41:40 +00:00
/ secret @username — Create "secret chat" with specified user
/ group @username groupname — Create group chat named groupname with @username
/ supergroup name description — Create supergroup chat
/ channel name description — Create channel
2019-04-16 14:07:15 +00:00
2019-04-16 13:24:03 +00:00
/ members — Supergroup members
2019-04-14 14:41:40 +00:00
/ history count — Retrieve chat history
/ search count query — Search in chat history
2019-04-16 14:07:15 +00:00
2019-04-14 14:41:40 +00:00
/ invite @username — Invite @username to current chat
/ kick @username — Remove @username from current chat
/ ban @username [hours] — Ban @username in current chat for [hours] hrs or forever if [hours] not specified
/ block — Blacklistscurrent user
/ unblock — Remove current user from blacklist
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
/ delete — Delete current chat
2019-04-16 14:07:15 +00:00
/ leave — Leave current chat
2019-04-16 04:00:09 +00:00
/ setusername username — Set username
/ setname First Last — Set name
/ setbio Bio — Set bio
/ setpassword old new — Set 2FA password (use "nil" for no password")
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
'
end
2019-05-07 19:22:26 +00:00
@xmpp . message ( @jid , chat_id . to_s , response ) if response
2019-04-06 06:12:54 +00:00
end
# processing outgoing message from queue #
2019-04-13 14:42:56 +00:00
def process_outgoing_msg ( chat_id , text )
2019-04-16 03:45:56 +00:00
@logger . info 'Sending message to Telegram chat %s...' % chat_id
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
# processing /commands #
2019-04-16 14:07:15 +00:00
return if not @cache [ :chats ] . key? chat_id # null chat
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
return self . process_command ( chat_id , text ) if text [ 0 ] == '/'
2019-04-08 22:28:26 +00:00
# handling replies #
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
if text [ 0 ] == '>' then
splitted = text . split ( " \n " )
2019-04-16 03:45:56 +00:00
reply_to = splitted [ 0 ] . scan ( / \ d / ) . join ( '' ) . to_i
reply_to = 0 if reply_to < 10000 # o_O
2019-04-08 22:28:26 +00:00
text = splitted . drop ( 1 ) . join ( " \n " ) if reply_to != 0
2019-04-13 14:42:56 +00:00
else
reply_to = 0
2019-04-08 22:28:26 +00:00
end
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
# handling files received from xmpp #
if text . start_with? @@content_upload_prefix then
message = TD :: Types :: InputMessageContent :: Document . new ( document : TD :: Types :: InputFile :: Remote . new ( id : text ) , caption : { :text = > '' , :entities = > [ ] } )
else
message = TD :: Types :: InputMessageContent :: Text . new ( :text = > { :text = > text , :entities = > [ ] } , :disable_web_page_preview = > false , :clear_draft = > true )
end
2019-04-08 22:28:26 +00:00
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
# send message and mark chat as read #
2019-04-08 22:28:26 +00:00
@client . send_message ( chat_id , message , reply_to_message_id : reply_to )
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
@client . view_messages ( chat_id , [ @cache [ :unread_msg ] . delete ( chat_id ) ] , force_read : true ) if @cache [ :unread_msg ] [ chat_id ]
2019-04-06 06:12:54 +00:00
end
2019-04-07 11:58:07 +00:00
# update users information and save it to cache #
2019-04-15 05:03:40 +00:00
def process_chat_info ( chat_id , subscription = true )
2019-04-09 06:42:42 +00:00
@logger . debug 'Updating chat id %s..' % chat_id . to_s
2019-04-07 11:58:07 +00:00
# fullfil cache.. pasha durov, privet. #
@client . get_chat ( chat_id ) . then { | chat |
@cache [ :chats ] [ chat_id ] = chat # cache chat
2019-04-14 14:41:40 +00:00
@client . download_file ( chat . photo . small . id ) . wait if chat . photo # download userpic
@cache [ :userpics ] [ chat_id ] = Digest :: SHA1 . hexdigest ( IO . binread ( self . format_content_link ( chat . photo . small . remote . id , 'image.jpg' , true ) ) ) if chat . photo and File . exist? self . format_content_link ( chat . photo . small . remote . id , 'image.jpg' , true ) # cache userpic
2019-05-04 20:17:29 +00:00
@xmpp . presence ( @jid , chat_id . to_s , :subscribe , nil , nil , @cache [ :chats ] [ chat_id ] . title . to_s ) if subscription # send subscription request
@xmpp . presence ( @jid , chat_id . to_s , nil , :chat , @cache [ :chats ] [ chat_id ] . title . to_s , nil , @cache [ :userpics ] [ chat_id ] ) if chat . type . instance_of? TD :: Types :: ChatType :: BasicGroup or chat . type . instance_of? TD :: Types :: ChatType :: Supergroup # send :chat status if its group/supergroup
2019-04-15 05:03:40 +00:00
# self.process_user_info(chat.type.user_id) if chat.type.instance_of? TD::Types::ChatType::Private # process user if its a private chat
2019-04-07 11:58:07 +00:00
} . wait
end
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
# update user info in cache and sync status to roster if needed #
2019-04-07 11:58:07 +00:00
def process_user_info ( user_id )
2019-04-09 06:42:42 +00:00
@logger . debug 'Updating user id %s..' % user_id . to_s
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
@client . get_user ( user_id ) . then { | user |
@cache [ :users ] [ user_id ] = user # add to cache
self . process_status_update ( user_id , user . status ) # status update
} . wait
2019-04-13 14:42:56 +00:00
@client . get_user_full_info ( user_id ) . then { | user_info |
2019-04-15 05:03:40 +00:00
@cache [ :users_fullinfo ] [ user_id ] = user_info # here is user "bio"
2019-04-13 14:42:56 +00:00
} . wait
2019-04-07 11:58:07 +00:00
end
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
2019-04-07 11:58:07 +00:00
# convert telegram status to XMPP one
2019-05-07 19:22:26 +00:00
def process_status_update ( user_id , status , immed = true )
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
@logger . debug " Processing status update for user id %s.. " % user_id . to_s
2019-04-14 14:41:40 +00:00
xmpp_show , xmpp_status , xmpp_photo = nil
2019-04-07 11:58:07 +00:00
case status
when TD :: Types :: UserStatus :: Online
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
xmpp_show = nil
xmpp_status = " Online "
2019-04-07 11:58:07 +00:00
when TD :: Types :: UserStatus :: Offline
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
xmpp_show = ( Time . now . getutc . to_i - status . was_online . to_i < 3600 ) ? :away : :xa
2019-05-04 20:17:29 +00:00
xmpp_status = DateTime . strptime ( ( status . was_online + Time . now . getlocal ( @timezone ) . utc_offset ) . to_s , '%s' ) . strftime ( " Last seen at %H:%M %d/%m/%Y " )
2019-04-07 11:58:07 +00:00
when TD :: Types :: UserStatus :: Recently
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
xmpp_show = :dnd
xmpp_status = " Last seen recently "
2019-04-07 11:58:07 +00:00
when TD :: Types :: UserStatus :: LastWeek
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
xmpp_show = :unavailable
xmpp_status = " Last seen last week "
2019-04-07 11:58:07 +00:00
when TD :: Types :: UserStatus :: LastMonth
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
xmpp_show = :unavailable
xmpp_status = " Last seen last month "
2019-04-07 11:58:07 +00:00
end
2019-04-14 14:41:40 +00:00
xmpp_photo = @cache [ :userpics ] [ user_id ] if @cache [ :userpics ] . key? user_id
2019-05-07 19:22:26 +00:00
@xmpp . presence ( @jid , user_id . to_s , nil , xmpp_show , xmpp_status , nil , xmpp_photo , immed )
2019-04-13 14:42:56 +00:00
end
# get contact information (for vcard).
def get_contact_info ( chat_id )
return if not @cache [ :chats ] . key? chat_id # no such chat #
username , firstname , lastname , phone , bio , userpic = nil
title = @cache [ :chats ] [ chat_id ] . title # <FN>
# user information
if @cache [ :users ] . key? chat_id then # its an user
firstname = @cache [ :users ] [ chat_id ] . first_name # <N/GIVEN>
lastname = @cache [ :users ] [ chat_id ] . last_name # <N/FAMILY>
username = @cache [ :users ] [ chat_id ] . username # <NICKNAME>
phone = @cache [ :users ] [ chat_id ] . phone_number # <TEL>
2019-04-15 05:03:40 +00:00
bio = @cache [ :users_fullinfo ] [ chat_id ] . bio if @cache [ :users_fullinfo ] . key? chat_id # <DESC>
2019-04-13 14:42:56 +00:00
end
# userpic #
if @cache [ :chats ] [ chat_id ] . photo then # we have userpic
userpic = self . format_content_link ( @cache [ :chats ] [ chat_id ] . photo . small . remote . id , 'image.jpg' , true )
2019-04-14 14:41:40 +00:00
userpic = ( File . exist? userpic ) ? Base64 . encode64 ( IO . binread ( userpic ) ) : nil
2019-04-13 14:42:56 +00:00
end
# ..
2019-04-14 14:41:40 +00:00
return title , username , firstname , lastname , phone , bio , userpic
2019-04-13 14:42:56 +00:00
end
2019-05-07 19:22:26 +00:00
# resolve id by @username (or just return id)
def resolve_username ( username )
resolved = nil
if username [ 0 ] == '@' then # @username
@client . search_public_chat ( username [ 1 .. - 1 ] ) . then { | chat | resolved = chat . id } . wait
elsif username [ 0 .. 3 ] == 'http' or username [ 0 .. 3 ] == 't.me' then # chat link
@client . join_chat_by_invite_link ( username )
elsif username . to_i != 0 then # user id
resolved = username
end
return '' if not resolved
return '@' + resolved . to_s
end
Release v0.3
[UPD] more correct way to handle user/chat/auth updates
[UPD] removed successful login message, using presence instead
[NEW] config option `content_upload_prefix` that says which url prefixes will be treated as media and uploaded as file to telegram
[NEW] processing self outgoing messages that sent via another telegram instance
[NEW] added gif animations, locations, video, chat events support
[NEW] control commands implemented:
/s/old/new (edit), /d (delete), /info @username, /add @username or uid, /join invite.link or id, /invite @username, /kick @username, /ban @username [hours], /block, /unblock, /leave, /delete
2019-04-12 03:11:56 +00:00
###########################################
## Format functions #######################
###########################################
2019-04-07 11:58:07 +00:00
# format tg user name #
2019-04-16 13:24:03 +00:00
def format_username ( user_id , show_id = false )
2019-04-14 14:41:40 +00:00
return if user_id == 0 # @me
2019-04-14 02:56:29 +00:00
if not @cache [ :users ] . key? user_id then self . process_user_info ( user_id ) end # update cache
if not @cache [ :users ] . key? user_id then return user_id end # return id if not found anything about this user
id = ( @cache [ :users ] [ user_id ] . username == '' ) ? user_id : @cache [ :users ] [ user_id ] . username # username or user id
name = @cache [ :users ] [ user_id ] . first_name # firstname
name = name + ' ' + @cache [ :users ] [ user_id ] . last_name if @cache [ :users ] [ user_id ] . last_name != '' # lastname
2019-04-16 13:24:03 +00:00
id = " %s ID %s " % [ id , user_id ] if show_id
2019-04-14 02:56:29 +00:00
return " %s (@%s) " % [ name , id ]
end
# format tg chat name #
def format_chatname ( chat_id )
2019-04-15 05:03:40 +00:00
if not @cache [ :chats ] . key? chat_id then self . process_chat_info ( chat_id , false ) end
2019-04-14 02:56:29 +00:00
if not @cache [ :chats ] . key? chat_id then return chat_id end
name = '%s (%s)' % [ @cache [ :chats ] [ chat_id ] . title , chat_id ]
2019-04-07 11:58:07 +00:00
return name
end
2019-04-14 02:56:29 +00:00
# format reply#
def format_reply ( chat_id , message_id )
text = ''
@client . get_message ( chat_id , message_id ) . then { | message | text = " %s " % message . content . text . text . to_s } . wait
text = ( text . lines . count > 1 ) ? " %s... " % text . split ( " \n " ) [ 0 ] : text
2019-04-14 14:41:40 +00:00
return " %s (%s..) " % [ message_id . to_s , text ]
2019-04-14 02:56:29 +00:00
end
2019-04-07 11:58:07 +00:00
# format content link #
2019-04-13 14:42:56 +00:00
def format_content_link ( file_id , fname , local = false )
prefix = ( local ) ? @@content_path : @@content_link
path = " %s/%s%s " % [ prefix , Digest :: SHA256 . hexdigest ( file_id ) , File . extname ( fname ) ]
2019-04-07 11:58:07 +00:00
return path
end
2019-04-14 02:56:29 +00:00
2019-05-04 21:21:59 +00:00
# state functions #
2019-05-04 20:17:29 +00:00
def online? ( ) @online end
def tz_set? ( ) return @timezone != '-00:00' end
2019-04-06 06:12:54 +00:00
end