/// <summary>
        /// Polls for new messages (activities).
        /// </summary>
        /// <param name="conversationId">The ID of the conversation.</param>
        /// <returns></returns>
        public async Task PollMessagesAsync(string conversationId)
        {
            if (string.IsNullOrEmpty(conversationId))
            {
                return;
            }

            ActivitySet activitySet = null;

            using (DirectLineClient directLineClient = new DirectLineClient(_directLineSecret))
            {
                var watermark    = _conversationCache.GetConversation(new IdAndTimestamp(conversationId))?.ActivityWaterMark;
                var conversation = directLineClient.Conversations.ReconnectToConversation(conversationId);
                activitySet = await directLineClient.Conversations.GetActivitiesAsync(conversationId, watermark);

                // XXX WarterMarkこの段階で処理してしまうのはちょっと嫌。
                _conversationCache.PutConversation(
                    new IdAndTimestamp(conversationId),
                    new ConversationContext(conversation, activitySet?.Watermark));
            }

            if (activitySet != null)
            {
                Debug.WriteLine($"conversationId {conversationId} {activitySet.Activities?.Count} activity/activities received");
                if (activitySet.Activities?.Count > 0)
                {
                    Debug.WriteLine(JsonConvert.SerializeObject(activitySet.Activities));
                }

                // ボットへ送る方向のアクティビティは処理しない。
                // TODO このDirectLineClientではその方向のアクティビティに ReplyToId を指定していないためそれでフィルタしているが、恐らく正しいフィルタではない。
                var activities = activitySet.Activities
                                 .Where(x => !string.IsNullOrEmpty(x.ReplyToId))
                                 .ToList();

                if (_synchronizationContext != null)
                {
                    _synchronizationContext.Post((o) => ActivitiesReceived?.Invoke(this, activities), null);
                }
                else
                {
                    ActivitiesReceived?.Invoke(this, activities);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Polls for new messages (activities).
        /// </summary>
        /// <param name="conversationId">The ID of the conversation.</param>
        /// <returns></returns>
        public async Task PollMessagesAsync(string conversationId = null)
        {
            if (!string.IsNullOrEmpty(conversationId) || !string.IsNullOrEmpty(_conversation?.ConversationId))
            {
                conversationId = string.IsNullOrEmpty(conversationId) ? _conversation.ConversationId : conversationId;
                ActivitySet activitySet = null;

                using (DirectLineClient directLineClient = new DirectLineClient(_directLineSecret))
                {
                    directLineClient.Conversations.ReconnectToConversation(conversationId);
                    activitySet = await directLineClient.Conversations.GetActivitiesAsync(conversationId, _watermark);
                }

                if (activitySet != null)
                {
#if DEBUG
                    if (activitySet.Activities?.Count > 0)
                    {
                        System.Diagnostics.Debug.WriteLine($"{activitySet.Activities?.Count} activity/activities received");
                    }
#endif

                    _watermark = activitySet?.Watermark;

                    var activities = (from activity in activitySet.Activities
                                      select activity)
                                     .ToList();

                    if (_synchronizationContext != null)
                    {
                        _synchronizationContext.Post((o) => ActivitiesReceived?.Invoke(this, activities), null);
                    }
                    else
                    {
                        ActivitiesReceived?.Invoke(this, activities);
                    }
                }
            }
        }