-
Notifications
You must be signed in to change notification settings - Fork 9
Resources API
alexvolchetsky edited this page Jul 23, 2020
·
1 revision
SDK allows uploading of images and sounds programmatically in Alice skill.
For that functionality service Yandex.Alice.Sdk.Services.DialogsApiService
was created. Below you can find example of configuration and usage of that service.
Add in appsettings.json the following elements:
"AliceSettings": {
"SkillId": "",
"DialogsOAuthToken": ""
}
SkillId
should have ID of your skill. Here in notes there is info where to find skill ID.
DialogsOAuthToken
should have OAuth-token for dialogs. Here more info about getting of this token.
Creat class that will contain ID of your skill:
public class AliceSettings
{
public Guid SkillId { get; set; }
public AliceSettings(string skillId)
{
SkillId = Guid.Parse(skillId);
}
}
Add the following lines in your Startup
class
using Yandex.Alice.Sdk.Models.DialogsApi;
using Yandex.Alice.Sdk.Services;
public void ConfigureServices(IServiceCollection services)
{
...
var skillIdSection = Configuration.GetSection("AliceSettings:SkillId");
var aliceSettings = new AliceSettings(skillIdSection.Value);
var apiSettings = new DialogsApiSettings(Configuration.GetSection("AliceSettings:DialogsOAuthToken").Value);
services.AddSingleton(apiSettings);
services.AddSingleton<IDialogsApiService,DialogsApiService>();
services.AddSingleton(aliceSettings);
...
}
If you don't use DI then do initialization of DialogsApiService
as follows:
DialogsApiSettings dialogsApiSettings = new DialogsApiSettings("your-token");
IDialogsApiService dialogsApiService = new DialogsApiService(dialogsApiSettings);
[ApiController]
[Route("[controller]")]
public class AliceController : ControllerBase
{
private readonly IDialogsApiService _dialogsApiService;
private readonly AliceSettings _aliceSettings;
public AliceController(IDialogsApiService dialogsApiService, AliceSettings aliceSettings)
{
_dialogsApiService = dialogsApiService;
_aliceSettings = aliceSettings;
}
[HttpPost]
[Route("/alice")]
public async Task<IActionResult> Get(AliceRequest aliceRequest)
{
Uri uri = new Uri("your-image-url");
var request = new DialogsWebUploadRequest(uri);
var response = await _dialogsApiService.UploadImageAsync(_aliceSettings.SkillId, request).ConfigureAwait(false);
if (response.IsSuccess)
{
var aliceResponse = new AliceImageResponse(aliceRequest, "Изображение загружено");
aliceResponse.Response.Card = new AliceImageCardModel
{
Title = "Изображение загружено",
Description = "Вы можете попробовать загрузить другое изображение",
ImageId = response.Content.Image.Id,
};
aliceResponse.SessionState = new CustomSessionState(ModeType.ResourcesTesting);
return Ok(aliceResponse);
}
return Ok(new AliceResponse(aliceRequest, "Не удалось загрузить изображение"));
}
}