C# LINE Bot development with Azure Function for cross-platform
Tags: c, linebot, azurefunctions, serverless
Published: 2018-02-06 02:04:13

---
title: C# LINE Bot development with Azure Function for cross-platform
published: true
description: C# LINE Bot development with Azure Function for cross-platform
tags: C#,LINEBot,AzureFunction,Serverless
cover_image: https://thepracticaldev.s3.amazonaws.com/i/4hkscd6dgq71wzdhepdl.PNG
---

Serverless and bot are buzz words these days. But when you say bot in some countries like Taiwan, Korea, Japan, many people think about LINE as its platform. In this article, I explain how you can jump start developing LINE bot using Microsoft technologies  and host the app on Function App at the end.

# Prerequisites

Your preferred OS. I use Windows but this works for Mac and Linux too.

- LINE developer account. https://developers.line.me/en/
- Azure Subscription. If you don’t have it yet, free trial [here](https://azure.microsoft.com/en-us/free/)
- Visual Studio Code. Just grab the latest one [here](https://code.visualstudio.com/)
- dotnet core library. [Windows](https://www.microsoft.com/net/learn/get-started/windows), [MacOS](https://www.microsoft.com/net/learn/get-started/macos), [Linux](https://www.microsoft.com/net/learn/get-started/linux)
- Node.js. https://nodejs.org/en/
- Git. https://git-scm.com/ 
- ngrok. https://ngrok.com/

# Setup LINE

If you already setup your app, skip this.

1\. Go to https://developers.line.me/en/ and signup for developer account. You need LINE account as well, but I guess you already have one anyway.

2\. Click “Start using Messaging API”.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb28.png)

3\. If you have a provider, select it. Otherwise, click + icon.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb29.png)

4\. Enter name and Add. Then click Next page.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb30.png)

5\. Provide app icon, name and description. DO NOT use “LINE” as part of app name. You shall see error later.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb31.png)

6\. For plan, select “Developer Trial” as we are developers. Pick appropriate category and subcategory. Enter email and click confirm.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb77.png)

7\. Then accept terms and condition as you wish, then click Create.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb33.png)

8\. Click “Configuration not yet complete”.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb34.png)

9\. You have all information needed in the configuration page. Go to “Messaging settings”, and click “Issue” for channel access token.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb35.png)

10\. In the popup, read it and click “Issue”. If you set it 0 hours, it won’t expire.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb36.png)

11\. Then enable “Use webhooks” and “Allow bot to join group chats”. Leave the Webhook URL for now.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb37.png)

12\. For “Using LINE@ feature” section, disable bot “Auto-reply” and “Greeting”.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb38.png)

13\. Finally you see QR code in the bottom, which you scan from your LINE application in iOS or Android so that the bot application is added.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/IMG_1908_thumb.png)

That’s it for now.


# Provision Azure environment

1\. Login to Azure portal https://portal.azure.com

2\. For house keeping, lets create resource group first, which host all resources we use later. Click “Resource Group”.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb39.png)

3\. Click [+ Add].

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb40.png)

4\. Enter name and select region, then click “Create” at the bottom of the page.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb41.png)

5\. Once created, refresh the list and select the resource group you created.

6\. Click [+ Add] so that we can add actual resource.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb42.png)

7\. Type “function” and search bar, and select “Function App”. It opens a “blade” on the right, so click “Create”.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb43.png)

8\. Enter mandatory fields and create it. I did like following. One key part is that let it create Storage for us.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb44.png)

9\. Once completed, back to resource group page, select the resource group and confirm you have three resources now.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb45.png)

Keep the browser open for later use.

# Azure Functions app runtime

As we develop function app, we need runtime.

1\. Open command prompt or terminal.

2\. Run following command to install tools.

```shell
npm i -g azure-functions-core-tools@core
```
For Mac,
```shell
sudo npm i -g azure-functions-core-tools@core --unsafe-perm
```
3\. Run func to see if you installed successfully.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb46.png)

# Bot application code

There are two ways to get template code. We host C# SDK and sample templates for LINE messaging api at GitHub.com, and yeoman generator template.

## Prepare keys
In any case, it needs several keys. So let's get it first.

1\. Go to azure portal. Select storage account from the list, in my case “lineserverlessbab7d”.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb47.png)

2\. Select “Access Keys”.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb48.png)

3\. There are several items listed. We need one of “CONNECTION STRING”. Copy either one. I pick up the first one here.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb49.png)

4\. Then get LINE ChannelSecret and ChannelAccessToken. Go to LINE developer portal and grab it from there. Oh I show my keys? No worries, I will re-issue them before you notice.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb50.png)

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb51.png)

### Get code from Yeoman
1\. Run the following code to install yeoman and generator.
```shell
npm install -g yo
npm install -g generator-line-bot
```
for Mac you may need to run with sudo
```shell
sudo npm install -g yo
sudo npm install -g generator-line-bot
```

2\. Run yeoman to create project. Following code will create MyBot folder and generate solution by using C# and Azure Function template.

```shell
yo line-bot MyBot --csharp --function
```

3\. It will ask you keys. Enter keys which you get in previous steps.

4\. Once solution is generated, move to the created folder.

5\. Open local.settings.json to confirm all key information is stored correctly.

### Get code from GitHub

1\. Open command prompt or terminal. Create a directory and go there.

2\. Run following command to clone everything.

```shell
git clone https://github.com/pierre3/LineMessagingApi
```

3\. Once cloning completed, change directory to LineMessagingApi\FunctionAppSample.v2. Why? Because it contains the functions app we use. Or you wonder difference between v1 and v2? v1 is for Windows only and v2 is for cross-platform based on .NET core technology.

4\. Type code . to open the folder via Visual Studio Code.

5\. Open local.settings.json file. Fill the keys.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb52.png)

6\. From “View” menu, select “Integrated Terminal”.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb53.png)

7\. Type “dotnet build” to compile them all.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb54.png)

In both case, Most of the bot logic is in LineBotApp.cs file. Go ahead to modify as you want.


# Test it locally

There are several things we need to setup to test it locally.

1\. Open new command prompt or terminal and change directory to where you store ngrok.exe. If you already added PATH, then no worries.

2\. Run following command to open connection from ngrok server to local. Port 7071 is used by Function app.

```shell
ngrok.exe http --host-header=localhost:7071 7071
```

3\. Confirm the ngrok server address. In this case, “7cde9070.ngrok.io”.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb55.png)

4\. Go back to LINE developer portal and update “Webhook URL”. Don’t forget to add “/api/linebot” at the end where actually the app waits.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb56.png)

5\. Now run the app. In command prompt or terminal, go to application directory/bin/Debug/netstandard2.0. If you don’t see the directory, you forget to run “dotnet build” or something went wrong.

6\. Run the following command to start the function app.

```shell
func host start
```
7\. If you see “green” line which tells us where the app is waiting, we are ready.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb57.png)

8\. Next, let’s attach Visual Studio Code to the process. Go to Visual Studio Code and click “debug” icon.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb58.png)

9\. From drop down next to DEBUG, select “Add Configuration”.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb59.png)

10\. Type “core” and select .NET Core.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb60.png)

11\. Then select “Attach to local .NET Core Console App”.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb61.png)

12\. From dropdown, select “.NET Core Attach”. and press F5.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb62.png)

13\. From the list, select “dotnet.exe”. Then it attaches to the process.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb63.png)

# Debug

1\. First of all, send “Hi” from your LINE app. It should return the reply to LINE as template handles all the types.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/IMG_1909_thumb.png)

2\. Now put breakpoint anywhere. I put the break point at Run method in run.cs where the message comes in.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb64.png)

3\. Send message again from LINE client. And you see breakpoint hit.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb65.png)

# Publish the app

Okay, everything went as expect. Finally we can publish the app.

1\. Stop the function app first.

2\. In command prompt or terminal, type following to login to azure.

```shell
func azure login
```

3\. Follow the instruction to complete login.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb66.png)

4\. Open browser, go to the URL and put code.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb67.png)

5\. Once login completed, run the following command to publish it. You need to replace the Function App name for yours. The i options upload local settings to azure environment, and y does override it if exists.

```shell
func azure functionapp publish lineserverlessbot -i -y
```

6\. Once completed, go to Azure Portal and select Function App.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb68.png)

7\. Click “Application Settings”

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb69.png)

8\. Confirm three settings in local.settings.json is copied over to Application settings.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb70.png)

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb71.png)

9\. Select “Overview” and “Function app settings”. Select “beta” for “Runtime version”. This enables v2 function.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb72.png)

10\. Select LINEBOT function from the list.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb73.png)

11\. Click “Get function URL” on top right of the page to get endpoint URL.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb74.png)

12\. Copy the URL.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb75.png)

13\. Go back to LINE developer portal and update Webhook URL.

![image](https://msdnshared.blob.core.windows.net/media/2017/12/image_thumb76.png)

14\. Once you save everything, try from your LINE client to see if it works.

# Summary

Phew, everything went well! (at least for me). I know all the development start from here, but you have all the frame up and running.

In the next article, I introduce brand-new simulator so that we can boost our development cycle.

Ken