Important
This repository is no longer actively maintained and is preserved here as a public archive.
🚀 For ongoing work, please see line-openapi-dotnet, a project for generating client code from LINE's official OpenAPI specifications.
LINE Messaging API
日本語の説明はこちら
This is a C# implementation of the LINE Messaging API.
Getting Started
This repository contains SDK itself, as well as base samples and Visual Studio templates.
.Net Standard Class Library
Use NuGet manager to import the library to your project.
NuGet Gallery | Line.Messaging
Samples
There are several samples which uses the SDK. You can find detail instructions in each directory.
Visual Studio Templates
The template can be found in Market Place, but if you want to tweak it, the source is also available.
Usage
Basically, there are three steps to use the SDK.
- Instantiate LineMessagingClient.
- Implement a class which inherits WebhookApplication.
- Override the method to handle the event.
LineMessagingClient Class
This is a class to communicate with LINE Messaging API platform. It uses HttpClient-based asynchronous methods such as followings.
Task ReplyMessageAsync(string replyToken, IList<ISendMessage> messages)
Task ReplyMessageAsync(string replyToken, params string[] messages)
Task PushMessageAsync(string to, IList<ISendMessage> messages)
Task PushMessageAsync(string to, params string[] messages)
Task MultiCastMessageAsync(IList<string> to, IList<ISendMessage> messages)
Task MultiCastMessageAsync(IList<string> to, params string[] messages)
Task<ContentStream> GetContentStreamAsync(string messageId)
Task<UserProfile> GetUserProfileAsync(string userId)
Task<byte[]> GetContentBytesAsync(string messageId)
Task<UserProfile> GetGroupMemberProfileAsync(string groupId, string userId)
Task<UserProfile> GetRoomMemberProfileAsync(string roomId, string userId)
Task<IList<UserProfile>> GetGroupMemberProfilesAsync(string groupId)
Task<IList<UserProfile>> GetRoomMemberProfilesAsync(string roomId)
Task<GroupMemberIds> GetGroupMemberIdsAsync(string groupId, string continuationToken)
Task<GroupMemberIds> GetRoomMemberIdsAsync(string roomId, string continuationToken = null)
Task LeaveFromGroupAsync(string groupId)
Task LeaveFromRoomAsync(string roomId)
Parse and process Webhook-Events
Use GetWebhookEventsAsync extension method for incoming request to parse the LINE events from the LINE platform. See FunctionAppSample/HttpTriggerFunction.sc as an example.
using Line.Messaging;
using Line.Messaging.Webhooks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace FunctionAppSample
{
public static class HttpTriggerFunction
{
static LineMessagingClient lineMessagingClient;
static HttpTriggerFunction()
{
lineMessagingClient = new LineMessagingClient(System.Configuration.ConfigurationManager.AppSettings["ChannelAccessToken"]);
var sp = ServicePointManager.FindServicePoint(new Uri("https://api.line.me"));
sp.ConnectionLeaseTimeout = 60 * 1000;
}
[FunctionName("LineMessagingApiSample")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
IEnumerable<WebhookEvent> events;
try
{
//Parse Webhook-Events
var channelSecret = System.Configuration.ConfigurationManager.AppSettings["ChannelSecret"];
events = await req.GetWebhookEventsAsync(channelSecret);
}
catch (InvalidSignatureException e)
{
//Signature validation failed
return req.CreateResponse(HttpStatusCode.Forbidden, new { Message = e.Message });
}
try
{
var connectionString = System.Configuration.ConfigurationManager.AppSettings["AzureWebJobsStorage"];
var tableStorage = await LineBotTableStorage.CreateAsync(connectionString);
var blobStorage = await BlobStorage.CreateAsync(connectionString, "linebotcontainer");
//Process the webhook-events
var app = new LineBotApp(lineMessagingClient, tableStorage, blobStorage, log);
await app.RunAsync(events);
}
catch (Exception e)
{
log.Error(e.ToString());
}
return req.CreateResponse(HttpStatusCode.OK);
}
}
}
Process Webhook-events
Create a class which inherits WebhookApplication class, then overrides the method you want to handle the LINE evnet in your class.