Exemplo n.º 1
0
        private void SendImageAttachments(HashSet <long> receivers, KidsNoteContent content)
        {
            List <MemoryStream> attachmentStreams = new List <MemoryStream>();

            for (int i = 0; i < content.Attachments.Count; ++i)
            {
                var attach = content.Attachments[i];

                if (attach.Data == null)
                {
                    // TODO: 관리 사용자에게 통지.
                    // attachmentStreams.Add(null);
                }
                else
                {
                    if (IsImageAttachment(attach))
                    {
                        MemoryStream ms = new MemoryStream();
                        attach.Data.CopyTo(ms);
                        attachmentStreams.Add(ms);
                    }
                    else if (attach.Type == AttachmentType.VIDEO)
                    {
                        // Video Link 로 따로 보낸다. (chrome redirect)
                    }
                    else
                    {
                        // Telegram 으로는 Image 만 보낸다.
                        // otherAttachments.AddLast(new InputMediaDocument(media));

                        attachmentStreams.Add(null);
                    }
                }
            }

            List <Task <Message[]> > mediaTaskList = new List <Task <Message[]> >();

            foreach (var user in receivers)
            {
                LinkedList <InputMediaPhoto> photoAlbum = new LinkedList <InputMediaPhoto>();
                for (int i = 0; i < content.Attachments.Count; ++i)
                {
                    var attach = content.Attachments[i];
                    if (attachmentStreams[i] != null)
                    {
                        MemoryStream copied = new MemoryStream();
                        attachmentStreams[i].Seek(0, SeekOrigin.Begin);
                        attachmentStreams[i].CopyTo(copied);
                        copied.Seek(0, SeekOrigin.Begin);
                        InputMedia media = new InputMedia(copied, String.Format("{0}_{1}", i + 1, attach.Name));
                        photoAlbum.AddLast(new InputMediaPhoto(media));
                    }
                }

                if (photoAlbum.Count > 0)
                {
                    Task <Message[]> task = TheClient.SendMediaGroupAsync(photoAlbum, user);
                    try
                    {
                        // 메시지 순서가 섞이지 않도록 모두 보내질 때까지 대기.
                        task.Wait();
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Trace.WriteLine(e);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void SendResponse(List <ChatId> receivers, KidsNoteNotifyMessage kidsNoteMessage)
        {
            foreach (var each in kidsNoteMessage.NewContents)
            {
                foreach (var eachContent in each.Value)
                {
                    string text = FormatContent(each.Key, eachContent);
                    //TheClient.SendTextMessageAsync(text).Wait();

                    List <Task <Message> > taskList = new List <Task <Message> >();
                    foreach (var user in receivers)
                    {
                        taskList.Add(TheClient.SendTextMessageAsync(user, text));
                    }

                    // 메시지 순서가 섞이지 않도록 모두 보내질 때까지 대기.
                    foreach (var task in taskList)
                    {
                        task.Wait();
                    }

                    LinkedList <InputMediaPhoto> photoAlbum = new LinkedList <InputMediaPhoto>();
                    //LinkedList<InputMediaDocument> otherAttachments = new LinkedList<InputMediaDocument>();
                    if (eachContent.Attachments != null && eachContent.Attachments.Count > 0)
                    {
                        taskList.Clear();

                        System.Diagnostics.Trace.WriteLine(String.Format("Title : {0}", eachContent.Title));

                        int no = 0;
                        foreach (var attach in eachContent.Attachments)
                        {
                            if (attach.Data == null)
                            {
                                // TODO: 관리 사용자에게 통지.
                            }
                            InputMedia media = new InputMedia(attach.Data, String.Format("{0}_{1}", ++no, attach.Name));
                            if (attach.Type == AttachmentType.IMAGE)
                            {
                                photoAlbum.AddLast(new InputMediaPhoto(media));
                                System.Diagnostics.Trace.WriteLine(String.Format("Attachment {0} : {1}", no, attach.Name));
                            }
                            else
                            {
                                // Telegram 으로는 Image 만 보낸다.
                                // otherAttachments.AddLast(new InputMediaDocument(media));
                            }
                        }
                    }

                    if (photoAlbum.Count > 0)
                    {
                        List <Task <Message[]> > mediaTaskList = new List <Task <Message[]> >();
                        foreach (var user in receivers)
                        {
                            mediaTaskList.Add(TheClient.SendMediaGroupAsync(photoAlbum, user));
                        }

                        // 메시지 순서가 섞이지 않도록 모두 보내질 때까지 대기.
                        foreach (var task in mediaTaskList)
                        {
                            task.Wait();
                        }
                    }
                }
            }
        }