Exemplo n.º 1
0
 private void ForwardMessagesWithCommentAsyncInternal(TLSendMessage message1, TLForwardMessages message2, Action <TLUpdatesBase> callback1, Action <TLUpdatesBase> callback2, Action fastCallback, Action <TLRPCError> faultCallback)
 {
     if (message1 != null)
     {
         AddAsyncInternal("messages.sendMessages", Constants.MessageSendingInterval, message1, callback1, fastCallback, faultCallback, 10);
     }
     SendAsyncInternal("messages.forwardMessages", Constants.MessageSendingInterval, message2, callback2, fastCallback, faultCallback, 10);
 }
Exemplo n.º 2
0
 private void SendMessageAsyncInternal(TLSendMessage message, Action <TLUpdatesBase> callback, Action fastCallback, Action <TLRPCError> faultCallback)
 {
     SendAsyncInternal("messages.sendMessage", Constants.MessageSendingInterval, message, callback, fastCallback, faultCallback);
 }
Exemplo n.º 3
0
        public async Task <IMessage> SendPrivateChatMessage(int fromUserId, int peersUserId, TLSendMessage message)
        {
            IMessage result = null;
            var      data   = this.objectFactory.CreateMessageData();

            data.ToPeerIdType = 0;
            data.ToPeerId     = peersUserId;
            data.Message      = message.Message;
            data.FromId       = fromUserId;
            data.Date         = MTServer.Services.Utils().ToTelegramDate(DateTime.UtcNow);
            using (var store = this.serviceProvider.GetService <IMessageStore>())
            {
                data = await store.CreateMessage(data);
            }
            result = this.objectFactory.CreateMessage(data);



            return(result);
        }
        private void SendReply(TLInputPeerBase inputPeer, TLString message, TLInt msgId)
        {
            if (msgId == null)
            {
                return;
            }
            if (TLString.IsNullOrEmpty(message))
            {
                return;
            }

            var actionInfo = TLUtils.OpenObjectFromMTProtoFile <TLVector <TLActionInfo> >(_actionInfoSyncRoot, Constants.ActionQueueFileName) ?? new TLVector <TLActionInfo>();

            var count = actionInfo.Count;

            Log("send count=" + count);

            var peerChannel = inputPeer as TLInputPeerChannel;
            var readHistory = peerChannel != null
                ? (TLObject) new TLReadChannelHistory {
                Channel = new TLInputChannel {
                    ChannelId = peerChannel.ChatId, AccessHash = peerChannel.AccessHash
                }, MaxId = msgId
            }
                : new TLReadHistory {
                Peer = inputPeer, MaxId = msgId
            };

            var readHistoryActionInfo = new TLActionInfo();

            readHistoryActionInfo.SendBefore = new TLInt(0);
            readHistoryActionInfo.Action     = readHistory;

            actionInfo.Add(readHistoryActionInfo);

            var sendMessage = new TLSendMessage();

            sendMessage.Flags    = new TLInt(0);
            sendMessage.Peer     = inputPeer;
            sendMessage.Message  = message;
            sendMessage.RandomId = TLLong.Random();

            var sendMessageActionInfo = new TLActionInfo();

            sendMessageActionInfo.SendBefore = new TLInt(0);
            sendMessageActionInfo.Action     = sendMessage;

            actionInfo.Add(sendMessageActionInfo);

            TLUtils.SaveObjectToMTProtoFile(new object(), Constants.ActionQueueFileName, actionInfo);

            if (actionInfo.Count > 0)
            {
                var deviceInfoService   = new DeviceInfoService(GetInitConnection(), true, "InteractiveNotificationsBackgroundTask", _id);
                var eventAggregator     = new TelegramEventAggregator();
                var cacheService        = new InMemoryCacheService(eventAggregator);
                var updatesService      = new UpdatesService(cacheService, eventAggregator);
                var transportService    = new TransportService();
                var connectionService   = new ConnectionService(deviceInfoService);
                var publicConfigService = new MockupPublicConfigService();

                var manualResetEvent = new ManualResetEvent(false);
                Log("before init");
                var requestsToRemove = new List <TLObject>();
                var mtProtoService   = new MTProtoService(deviceInfoService, updatesService, cacheService, transportService, connectionService, publicConfigService);
                mtProtoService.Initialized += async(o, e) =>
                {
                    Log("init completed");

                    var actionsString = new StringBuilder();
                    foreach (var info in actionInfo)
                    {
                        actionsString.AppendLine(info.ToString());
                    }
                    Log(actionsString.ToString());

                    var       sendMessageActions = new List <TLObject>();
                    const int maxActionCount     = 10;
                    var       currentCount       = 0;
                    foreach (var ai in actionInfo)
                    {
                        if (TLUtils.IsValidAction(ai.Action) && currentCount < maxActionCount)
                        {
                            currentCount++;
                            sendMessageActions.Add(ai.Action);
                        }
                    }

                    if (sendMessageActions.Count > 0)
                    {
                        mtProtoService.SendActionsAsync(sendMessageActions,
                                                        (request, result) => // will be invoked for each sent action
                        {
                            requestsToRemove.Add(request);
                            var sendingMessages = mtProtoService.SendingMessages;
                            Log("send completed count=" + sendingMessages, () =>
                            {
                                if (sendingMessages == 0)
                                {
                                    _clearActionInfoFile = true;

                                    manualResetEvent.Set();
                                }
                            });
                        },
                                                        error =>
                        {
                            Log(string.Format("send error={0}\n{1}", error, error.Exception),
                                async() =>
                            {
                                await Task.Delay(TimeSpan.FromSeconds(1.0));
                                manualResetEvent.Set();
                            });
                        });
                    }
                    else
                    {
                        manualResetEvent.Set();
                    }
                };
                mtProtoService.InitializationFailed += (o, e) =>
                {
                    Log("init failed");

                    manualResetEvent.Set();
                };
                mtProtoService.Initialize();
#if DEBUG
                manualResetEvent.WaitOne();
#else
                manualResetEvent.WaitOne(15000);
#endif
                if (_clearActionInfoFile)
                {
                    Log("clear");
                    lock (_actionInfoSyncRoot)
                    {
                        var actions = actionInfo;

                        foreach (var o in requestsToRemove)
                        {
                            MTProtoService.RemoveActionInfoCommon(actions, o);
                        }

                        TLUtils.SaveObjectToMTProtoFile(_actionInfoSyncRoot, Constants.ActionQueueFileName, actions);
                    }
                }
            }
        }
Exemplo n.º 5
0
        public async Task <IMessageData> SendPrivateChatMessage(int fromUserId, int toUserId, TLSendMessage sendMessage)
        {
            IMessageData result = null;

            try
            {
                using (var chatSTore = this.store.GetPrivateChatDataStore())
                {
                    var chatData = await chatSTore.GetByParticipants(fromUserId, toUserId);

                    if (chatData == null)
                    {
                        chatData = await chatSTore.Create(fromUserId, toUserId, "");
                    }
                    if (chatData == null)
                    {
                        throw new Exception("Failed to create chat!");
                    }
                    using (var message_store = this.store.GetMessageStore())
                    {
                        result = await message_store.CreatePrivateChatMessage(fromUserId, toUserId, sendMessage.Message, Extensions.ToTelegramDate(DateTime.UtcNow));

                        if (result != null)
                        {
                            await MTServer.Bus.Publish(new Contracts.Messages.NewMessageCreated
                            {
                                FromUserId = result.FromId ?? 0,
                                ToPeerId   = result.ToPeerId,
                                Text       = result.Message,
                                Id         = result.Id,
                                Date       = result.Date
                            });
                        }
                    }
                }
            }
            catch (Exception err)
            {
                this.logger.LogError(
                    "An error occured while trying to 'SendPrivateMessage: \r\n{0}", err.GetBaseException().Message);
                throw;
            }
            return(result);
        }