public static ViberUserMessageEntity ToEntity(ReceiveMessageFromUserCallback callback, Guid userId) =>
 new ViberUserMessageEntity
 {
     Message     = callback.Message.Text,
     MessageType = callback.Message.Type,
     UserId      = userId
 };
        public static ViberUserEntity ToEntity(ReceiveMessageFromUserCallback callback)
        {
            var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

            return(new ViberUserEntity
            {
                Avatart = callback.Sender.Avatart,
                Country = callback.Sender.Country,
                CreatedTimestamp = now,
                UpdatedTimestamp = now,
                IsSubscribed = true,
                Language = callback.Sender.Language,
                SessionId = Guid.NewGuid().ToString(),
                Name = callback.Sender.Name,
                ViberId = callback.Sender.Id
            });
        }
Exemple #3
0
        private async Task SaveUserMessageAsync(ReceiveMessageFromUserCallback callback)
        {
            var senderEntity = await _userDataStore.FindAsync(x => x.ViberId == callback.Sender.Id).ConfigureAwait(false);

            if (senderEntity == null)
            {
                var userEntity = ViberUserFactory.ToEntity(callback);

                await _userDataStore.CreateAsync(userEntity).ConfigureAwait(false);

                await _dbContext.SaveChangesAsync().ConfigureAwait(false);

                senderEntity = userEntity;
            }

            var messageEntity = ViberUserMessageFactory.ToEntity(callback, senderEntity.Id);

            await _messageDataStore.CreateAsync(messageEntity).ConfigureAwait(false);

            await _messageDataStore.SaveAsync().ConfigureAwait(false);
        }
Exemple #4
0
        public async Task HandleUserMessageAsync(ReceiveMessageFromUserCallback callback)
        {
            var user = await _userDataStore.FindAsync(x => x.ViberId == callback.Sender.Id);

            if (user == null)
            {
                var entity = ViberUserFactory.ToEntity(callback);

                await _userDataStore.CreateAsync(entity).ConfigureAwait(false);

                await _userDataStore.SaveAsync().ConfigureAwait(false);
            }

            await SaveUserMessageAsync(callback);

            var client = await SessionsClient.CreateAsync();

            var response = await client.DetectIntentAsync(
                session : SessionName.FromProjectSession("viber-bot-jexkor", user.SessionId),
                queryInput : new QueryInput
            {
                Text = new TextInput
                {
                    Text         = callback.Message.Text,
                    LanguageCode = "uk-UA"
                }
            }
                );

            await _dialogflowResultDataStore.CreateAsync(new DialogflowResultEntity
            {
                Request  = callback.Message.Text,
                Response = response.QueryResult.FulfillmentText
            });

            await _dialogflowResultDataStore.SaveAsync();

            await _viberRestClient.SendMessage(
                $"{response.QueryResult.FulfillmentText}", callback.Sender.Id);
        }