microsoft / BotFramework-WebChat

A highly-customizable web-based client for Azure Bot Services.
https://www.botframework.com/
MIT License
1.58k stars 1.53k forks source link

dateFormatter(...) is not a function if message Timestamp older than 2 days #2998

Closed xakpc closed 4 years ago

xakpc commented 4 years ago

Version

I using CDN JavaScript version of webchat (see Additional context for code) <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>

versions

<meta name="botframework-directlinespeech:version" content="4.8.0">
<meta name="botframework-webchat:bundle:variant" content="full">
<meta name="botframework-webchat:bundle:version" content="4.8.0">
<meta name="botframework-webchat:core:version" content="4.8.0">
<meta name="botframework-webchat:ui:version" content="4.8.0">

Describe the bug

I use client.Conversations.SendConversationHistoryAsync(activity.Conversation.Id, transcript, ct) to restore 6 last messages in Conversation after (activity.Name == "webchat/join") triggered. Everything worked fine until recently when a new issue was discovered:

if message timestamp older than 2 days

  var date = DateTime.Now.AddDays(-5);
  msgUser.Timestamp = date; //coreRequest.Timestamp;

I recieve that error in browser after SendConversationHistoryAsync call on backend

TypeError: (0 , i.default)(...).dateFormatter(...) is not a function
    at t.default (webchat.js:2)
    at webchat.js:2
    at f (webchat.js:2)
    at Ji (webchat.js:2)
    at webchat.js:2
    at Ha (webchat.js:2)
    at Wa (webchat.js:2)
    at Rs (webchat.js:2)
    at Ps (webchat.js:2)
    at Ms (webchat.js:2)

otherwise

  var date = DateTime.Now.AddDays(-2);
  msgUser.Timestamp = date; //coreRequest.Timestamp;

everything works fine image

I presume it's somehow connected to date format of older messages

Steps to reproduce

  1. Get index.html from index.zip
  2. Place your own token on line 50
  3. Catch activity webchat/join in webhook
    if (activity.Name == "webchat/join")
                {
                    HostingEnvironment.QueueBackgroundWorkItem(async ct =>
                    {
                        var client = CreateClient(activity, credentials);
                        var transcript = await _coreTalkService.GetHistoryAsync(new BotToken(userToken, botName), activity);
                        if (transcript.Activities.Any())
                            await client.Conversations.SendConversationHistoryAsync(activity.Conversation.Id, transcript, ct);
                    });
                }
  4. Construct transcript object

    public async Task<Transcript> GetHistoryAsync(BotToken botToken, Activity referenceActivity)
        {
            var result = new Transcript(new List<Activity>());
    
            var activityUser = referenceActivity.From;
            var activityBot = referenceActivity.Recipient;
    
            using (var analytics = new AnalyticsClient())
            {
                var requests = ...
    
                int id = 0;
                foreach (var coreRequest in requests)
                {
                    var msgUser = (Activity)Activity.CreateMessageActivity();
                    msgUser.Id = $"{referenceActivity.Conversation.Id}|{(++id).ToString().PadLeft(7, '0')}";
                    msgUser.Conversation = referenceActivity.Conversation;
                    msgUser.ChannelId = referenceActivity.ChannelId;
                    msgUser.From = activityUser;
                    msgUser.Recipient = activityBot;
                    msgUser.Text = coreRequest.Message;
    
                    var date = DateTime.Now.AddDays(-2);
                    msgUser.Timestamp = date;//coreRequest.Timestamp;
    
                    var msgBot = msgUser.CreateReply(coreRequest.Response);
                    msgBot.Id = $"{referenceActivity.Conversation.Id}|{(++id).ToString().PadLeft(7, '0')}";
                    msgBot.Timestamp = date;//coreRequest.Timestamp.Value.AddSeconds(5);                    
    
                    if (msgUser.Text != "PUSH REQUEST")
                        result.Activities.Add(msgUser);
                    result.Activities.Add(msgBot);
                }
            }
    
            return result;
        }
  5. Change var date = DateTime.Now.AddDays(-2); to reproduce the problem

Expected behavior

I expect older messages with localized timestamps shown

Additional context

Web chat function

(async function () {
        // add hooks to connect event
        const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
          if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
            // When we receive DIRECT_LINE/CONNECT_FULFILLED action, we will send an event activity using WEB_CHAT/SEND_EVENT
            dispatch({
              type: 'WEB_CHAT/SEND_EVENT',
              payload: {
                name: 'webchat/join',
                value: { language: window.navigator.language }
              }
            });
          }

           if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
                //console.log(action);              
           }

          return next(action);
        });

            function customRenderMarkdown(text)
            {
            console.log(text);  
            const options = { markdownRespectCRLF: true };
            var rendered = renderMarkdown(text, options);
            console.log(rendered);  
            return rendered;
            }

         window.WebChat.renderWebChat(
            {
               directLine: window.WebChat.createDirectLine({
                  token: '...'
               }),
               store,
               renderMarkdown: customRenderMarkdown,               
               userID: '123web', // TODO: change to proper ID
               username: 'test@user.name', // TODO: change to proper Name
               locale: 'en-US',
               // стиль https://github.com/Microsoft/BotFramework-WebChat/blob/master/packages/component/src/Styles/defaultStyleOptions.js
               styleOptions: {
                botAvatarInitials: 'B',
                userAvatarInitials: 'U',
                hideUploadButton: true
               }              
            },
            document.getElementById('webchat'));            
            document.querySelector('#webchat > *').focus();

         })().catch(err => console.error(err)); 

[Bug]

CoHealer commented 4 years ago

@stevkan, please attempt to repro.

stevkan commented 4 years ago

@xakpc, I'm trying to repro your issue, but I'm running into a separate issue. How are you loading the HostingEnvironment class into the project? BotFramework v4 is built on .NET Core, but the System.Web.Hosting package is built for .NET Framework.

cleemullins commented 4 years ago

@xakpc Can you please answer @stevkan, so that he can get a repo in place? If we don't hear back in the next day or two, we'll close this due to inactivity.

xakpc commented 4 years ago

@stevkan I use ASP.NET Web API app on .net framework 4.7.2 as my backend service. But that actually doesn't matter. I revisited the problem and build a small project to reproduce it based on ASP.NET Core Web API

BotFramework.Reproduce.zip

stevkan commented 4 years ago

@xakpc, thank you for your patience. I've continued to try to repro your issue using the web chat index.html file you provided as well as the "BotFramework.Reproduce" project files. The index.html works well enough, however I'm struggling some little bit with the project. For some reason I have yet to figure out, I can't get the bot to recognize any activity is being passed to it.

I'll continue to dig into this and, hopefully, have something more concrete to report back to you before too long.

tonyanziano commented 4 years ago

@stevkan This is related to #3039 . That issue might provide some more context for helping you repro the issue.

stevkan commented 4 years ago

@xakpc, Is this still an issue for you?

If it is, is this reproducible using the echo bot, or similar? The files you sent don't appear to be a typical bot construction. As it is, I can't test or reproduce using what you provided.

Also, as mentioned above in the linked issue, there is a known bug in dateFormatter that could be tied to the error you are receiving. Can you verify the language format that is being used and that you aren't suffering from the same problem as detailed there?

stevkan commented 4 years ago

Closing due to the lack of response from customer since customer's last activity. If the issue persists, please feel free to re-open.

jelmuei commented 4 years ago

@stevkan Not sure if this helps but I can't successfully load transcript files in the bot emulator older than 2 days. A work around for me was to change the system time of my windows machine to a date before the timestamp of the conversation and to restart the bot emulator.

You can reproduce it with one of the official transcript files which will not load (white screen of the webchat) in the latest bot emulator. https://microsoft.github.io/botframework-solutions/skills/samples/transcripts/