.NET6 C#, LineBot, Line Messaging API, C#, dotnet core
Hello 大家好,今天的這篇文章,會把Line Bot Messaging 相關的API 介紹到一段落了,當然 Line Bot 還有不少功能還來不及介紹/實作。不過一路看到這邊的各位應該也對官方文件越來越熟悉了吧,還有很多有趣的內容可以留給各位自行探索~
當然真的很想要我們來介紹的話 可以留言讓我們知道,有機會的話再幫各位補上。
那今天這篇的內容就是走完 rich menu 剩下的 API,讓我們開始吧。
尚未實作的 API 共有下幾支 :
private readonly string getRichMenuUri = "https://api.line.me/v2/bot/richmenu/{0}";
private readonly string defaultRichMenuUri = "https://api.line.me/v2/bot/user/all/richmenu";
// 個人的 linked rich menu 操作
private readonly string linkedRichMenuOfUserUri = "https://api.line.me/v2/bot/user/{0}/richmenu";
// 多人的 linked rich menu 操作
private readonly string linkedRichMenuOfMultipleUserUri = "https://api.line.me/v2/bot/richmenu/bulk/";
Line 提供之 API 會將我們上傳的 rich menu image 檔案內容傳回,格式與上傳時相同,皆是 Byte[] 類別,我這裡不做特別的處理,一樣直接將檔案內容傳出,使 swagger 上可直接看到圖片就好。
文件內容
RichMenuService
public async Task<FileContentResult> DownloadRichMenuImage(string richMenuId)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", channelAccessToken);
// 直接取得回傳的 Byte Array
var bytes = await client.GetByteArrayAsync(String.Format(richMenuImageUri, richMenuId));
//
return new FileContentResult(bytes, "image/png");
}
[HttpGet("RichMenu/DownloadImage/{richMenuId}")]
public async Task<FileContentResult> DownloadRichMenuImageById(string richMenuId)
{
return await _richMenuService.DownloadRichMenuImage(richMenuId);
}
使用 Message api 的話,一個 Line Bot 最多可以存放 1000 張 rich menu 在 Line 的平台中,若存滿時,則需刪除多餘的 rich menu 才能再新增 rich menu。
文件內容
RichMenuService
public async Task<string> DeleteRichMenu(string richMenuId)
{
var request = new HttpRequestMessage(HttpMethod.Delete, string.Format(getRichMenuUri, richMenuId));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", channelAccessToken);
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
[HttpDelete("RichMenu/Delete/{richMenuId}")]
public async Task<IActionResult> DeleteRichMenu(string richMenuId)
{
return Ok(await _richMenuService.DeleteRichMenu(richMenuId));
}
以 richMenuId 搜尋,回傳該 rich menu 上傳的格式內容。
文件內容
RichMenuService
// 回傳物件直接使用 RichMenuDto,就不另外宣告了。
public async Task<RichMenuDto> GetRichMenuById(string richMenuId)
{
var request = new HttpRequestMessage(HttpMethod.Get, string.Format(getRichMenuUri, richMenuId));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", channelAccessToken);
var response = await client.SendAsync(request);
var richMenu = _jsonProvider.Deserialize<RichMenuDto>(await response.Content.ReadAsStringAsync());
return richMenu;
}
[HttpGet("RichMenu/Get/{richMenuId}")]
public async Task<IActionResult> GetRichMenuById(string richMenuId)
{
return Ok(await _richMenuService.GetRichMenuById(richMenuId));
}
取得目前設為 default rich menu 的 richMenuId。
文件內容
RichMenuService
public async Task<RichMenuDto> GetDefaultRichMenuId()
{
var request = new HttpRequestMessage(HttpMethod.Get, defaultRichMenuUri);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", channelAccessToken);
var response = await client.SendAsync(request);
return _jsonProvider.Deserialize<RichMenuDto>(await response.Content.ReadAsStringAsync());
}
[HttpGet("RichMenu/Default/GetId")]
public async Task<IActionResult> GetDefaultRichMenuId()
{
return Ok(await _richMenuService.GetDefaultRichMenuId());
}
取消目前 default rich menu 的設定。
文件內容
RichMenuService
public async Task<string> CancelDefaultRichMenu()
{
var request = new HttpRequestMessage(HttpMethod.Delete, defaultRichMenuUri);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", channelAccessToken);
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
[HttpGet("RichMenu/Default/Cancel")]
public async Task<IActionResult> CancelDefaultRichMenu()
{
return Ok(await _richMenuService.CancelDefaultRichMenu());
}
取得目前綁定給該 User 的 richMenuId。
文件內容
RichMenuService
public async Task<RichMenuDto> GetRichMenuIdLinkedToUser(string userId)
{
var request = new HttpRequestMessage(HttpMethod.Get, string.Format(linkedRichMenuOfUserUri, userId));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", channelAccessToken);
var response = await client.SendAsync(request);
return _jsonProvider.Deserialize<RichMenuDto>(await response.Content.ReadAsStringAsync());
}
[HttpGet("RichMenu/GetLinkedId/{userId}")]
public async Task<IActionResult> GetRichMenuIdLinkedToUser(string userId)
{
return Ok(await _richMenuService.GetRichMenuIdLinkedToUser(userId));
}
Link api 文件
Unlink api 文件
RichMenuService
public async Task<string> LinkRichMenuToUser(string userId, string richMenuId)
{
var request = new HttpRequestMessage(HttpMethod.Post, string.Format(linkedRichMenuOfUserUri + "/{1}", userId, richMenuId));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", channelAccessToken);
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
public async Task<string> UnlinkRichMenuFromUser(string userId)
{
var request = new HttpRequestMessage(HttpMethod.Delete, string.Format(linkedRichMenuOfUserUri, userId));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", channelAccessToken);
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
[HttpGet("RichMenu/Link/{userId}/{richMenuId}")]
public async Task<IActionResult> LinkRichMenuToUser(string userId, string richMenuId)
{
return Ok(await _richMenuService.LinkRichMenuToUser(userId, richMenuId));
}
[HttpDelete("RichMenu/Unlink/{userId}")]
public async Task<IActionResult> UnlinkRichMenuFromUser(string userId)
{
return Ok(await _richMenuService.UnlinkRichMenuFromUser(userId));
}
public async Task<string> LinkRichMenuToMultipleUser(LinkRichMenuToMultipleUserDto dto)
{
var request = new HttpRequestMessage(HttpMethod.Post, linkedRichMenuOfMultipleUserUri + "link")
{
Content = new StringContent(_jsonProvider.Serialize(dto), Encoding.UTF8,"application/json")
};
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", channelAccessToken);
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
public async Task<string> UnlinkRichMenuFromMultipleUser(LinkRichMenuToMultipleUserDto dto)
{
var request = new HttpRequestMessage(HttpMethod.Post, linkedRichMenuOfMultipleUserUri + "unlink")
{
Content = new StringContent(_jsonProvider.Serialize(dto), Encoding.UTF8, "application/json")
};
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", channelAccessToken);
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
[HttpPost("RichMenu/Link/Multiple")]
public async Task<IActionResult> LinkRichMenuToMultipleUser(LinkRichMenuToMultipleUserDto dto)
{
return Ok(await _richMenuService.LinkRichMenuToMultipleUser(dto));
}
[HttpPost("RichMenu/Unlink/Multiple")]
public async Task<IActionResult> UnlinkRichMenuFromMMultipleUser(LinkRichMenuToMultipleUserDto dto)
{
return Ok(await _richMenuService.UnlinkRichMenuFromMultipleUser(dto));
}
整套關於 rich menu 的 API 都實作完了,也代表著本系列中規劃的 Line Bot Messaging API 內容告一段落了,接下來會依照 Line Login、LIFF、Line Pay 的順序來繼續介紹 Line 的其它服務囉~ 下次見。
如果想要參考今天範例程式碼的部份,下面是 Git Repo 連結,方便大家參考。