All Projects → pierre3 → Linemessagingapi

pierre3 / Linemessagingapi

Licence: mit
LINE Messaging API for .Net

Projects that are alternatives of or similar to Linemessagingapi

Foxoffice
Sample application demonstrating how to build a distributed cloud .NET Core application based on CQRS and Event Sourcing.
Stars: ✭ 33 (-74.22%)
Mutual labels:  azure-functions
Hunt
Hunt is a virtual scavenger hunt app where players can join a game, select a team and solve hints to acquire treasure. The team with the most points wins.
Stars: ✭ 65 (-49.22%)
Mutual labels:  azure-functions
Serverless Url Shortener
Azure Function for a URL shortening website. Uses serverless functions, Azure Table Storage and Application Insights.
Stars: ✭ 113 (-11.72%)
Mutual labels:  azure-functions
Azure Functions Eventgrid Extension
EventGrid extension for Azure Functions
Stars: ✭ 41 (-67.97%)
Mutual labels:  azure-functions
Developing Solutions Azure Exam
This repository contains resources for the Exam AZ-203: Developing Solutions for Microsoft Azure. You can find direct links to resources and and practice resources to test yourself ☁️🎓📚
Stars: ✭ 59 (-53.91%)
Mutual labels:  azure-functions
Shopathome
Choose from Angular, React, Svelte, and Vue applications with an Azure Functions API, that deploys to Azure Static Web Apps
Stars: ✭ 73 (-42.97%)
Mutual labels:  azure-functions
Serverlessazurefriday
Serverless geo-replicated event-based architecture sample for Azure Friday
Stars: ✭ 17 (-86.72%)
Mutual labels:  azure-functions
Serverlesslibrary
Source code for the Azure Serverless Community Library
Stars: ✭ 119 (-7.03%)
Mutual labels:  azure-functions
Functions Csharp Eventhub Ordered Processing
Example of processing events in order with the Azure Functions Event Hubs trigger
Stars: ✭ 60 (-53.12%)
Mutual labels:  azure-functions
Serverless
⚡ Serverless Framework – Build web, mobile and IoT applications with serverless architectures using AWS Lambda, Azure Functions, Google CloudFunctions & more! –
Stars: ✭ 41,584 (+32387.5%)
Mutual labels:  azure-functions
Azure Functions Billing
Azure Functions v2 with .NET Core - billing in serverless architecture.
Stars: ✭ 49 (-61.72%)
Mutual labels:  azure-functions
Aggregator Cli
A new version of Aggregator aiming at Azure DevOps (ex Visual Studio Team Services)
Stars: ✭ 54 (-57.81%)
Mutual labels:  azure-functions
Azure Iot Developer Kit
A curated list of awesome Azure IoT Developer Kit projects and resources.
Stars: ✭ 73 (-42.97%)
Mutual labels:  azure-functions
Azure For Developers Workshop
The Azure cloud is huge and the vast service catalog may appear daunting at first, but it doesn’t have to be!
Stars: ✭ 38 (-70.31%)
Mutual labels:  azure-functions
Azure Functions Rs
Create Azure Functions with Rust!
Stars: ✭ 117 (-8.59%)
Mutual labels:  azure-functions
Cadscenario personalisation
This is a end to end Personalisation business scenario
Stars: ✭ 10 (-92.19%)
Mutual labels:  azure-functions
Serverless Graphql Workshop
GraphQL and Serverless workshop
Stars: ✭ 70 (-45.31%)
Mutual labels:  azure-functions
Azure Functions Host
The host/runtime that powers Azure Functions
Stars: ✭ 1,650 (+1189.06%)
Mutual labels:  azure-functions
Heroes Angular Serverless
TypeScript Node/Express 👉TypeScript Serverless ➕Angular
Stars: ✭ 119 (-7.03%)
Mutual labels:  azure-functions
Azurefunctionsintroduction
Sample Code for Azure Functions
Stars: ✭ 88 (-31.25%)
Mutual labels:  azure-functions

LINE Messaging API

NuGet NuGet

日本語の説明はこちら

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.

public abstract class WebhookApplication
{
    public async Task RunAsync(IEnumerable<WebhookEvent> events);
    
    protected virtual Task OnMessageAsync(MessageEvent ev);
    protected virtual Task OnJoinAsync(JoinEvent ev);
    protected virtual Task OnLeaveAsync(LeaveEvent ev);
    protected virtual Task OnFollowAsync(FollowEvent ev);
    protected virtual Task OnUnfollowAsync(UnfollowEvent ev);
    protected virtual Task OnBeaconAsync(BeaconEvent ev);
    protected virtual Task OnPostbackAsync(PostbackEvent ev);
    protected virtual Task OnAccountLinkAsync(AccountLinkEvent ev);
    protected virtual Task OnMemberJoinAsync(MemberJoinEvent ev);
    protected virtual Task OnMemberLeaveAsync(MemberLeaveEvent ev);
    protected virtual Task OnDeviceLinkAsync(DeviceLinkEvent ev);
    protected virtual Task OnDeviceUnlinkAsync(DeviceUnlinkEvent ev);
}

Finally, instantiate the class and run RunAsync method by giving the parsed LINE events as shown above.

See Line.Messaging/Webhooks/WebhookApplication.cs as processing event class.

class LineBotApp : WebhookApplication
{
  private LineMessagingClient MessagingClient { get; }
  private TraceWriter Log { get; }
  
  public WebhookApplication(LineMessagingClient lineMessagingClient,TraceWriter log)
  {
      MessagingClient = lineMessagingClient;
      Log = log;
  }

  protected override async Task OnMessageAsync(MessageEvent ev)
  {
    Log.Info($"SourceType:{ev.Source.Type},SourceId:{ev.Source.Id}");
    switch (ev.Message)
    {
      case TextEventMessage textMessage:
        await MessagingClient.ReplyMessageAsync(ev.ReplyToken, textMessage.Text);
        break;
      case ImageEventMessage imageMessage:
        //...
        break;
      case AudioEventMessage audioEventMessage:
        //...
        break;
      case VideoEventMessage videoMessage:
        //...
        break;
      case FileEventMessage fileMessage:
        //...
        break;
      case LocationEventMessage locationMessage:
        //...
        break;
      case StickerEventMessage stickerMessage:
        //...         
        break;
    }
  }
}

See each samples for more detail.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].