Example #1
0
        private async Task SubscribeUserAsync(SubscribedCallback callback)
        {
            if (await _userDataStore.ExistAsync(x => x.ViberId == callback.User.Id).ConfigureAwait(false))
            {
                return;
            }

            var entity = ViberUserFactory.ToEntity(callback);

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

            await _userDataStore.SaveAsync().ConfigureAwait(false);
        }
Example #2
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);
        }
Example #3
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);
        }