Skip to content

Commit

Permalink
Supports the rich menu api
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre3 committed Nov 4, 2017
1 parent 07d4eec commit d5cad54
Show file tree
Hide file tree
Showing 17 changed files with 459 additions and 51 deletions.
19 changes: 11 additions & 8 deletions FunctionAppSample/HttpTriggerFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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;
Expand All @@ -30,17 +29,16 @@ public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLeve
var channelSecret = System.Configuration.ConfigurationManager.AppSettings["ChannelSecret"];
var events = await req.GetWebhookEventsAsync(channelSecret);

//var connectionString = System.Configuration.ConfigurationManager.AppSettings["AzureWebJobsStorage"];
//var eventSourceState = await TableStorage<EventSourceState>.CreateAsync(connectionString, "eventsourcestate");
//var blobStorage = await BlobStorage.CreateAsync(connectionString, "linebotcontainer");
//var app = new LineBotApp(lineMessagingClient, eventSourceState, blobStorage, log);
var connectionString = System.Configuration.ConfigurationManager.AppSettings["AzureWebJobsStorage"];
var eventSourceState = await TableStorage<EventSourceState>.CreateAsync(connectionString, "eventsourcestate");
var blobStorage = await BlobStorage.CreateAsync(connectionString, "linebotcontainer");
var app = new LineBotApp(lineMessagingClient, eventSourceState, blobStorage, log);

//Samples app
//var app = new DateTimePickerSampleApp(lineMessagingClient, log);
//var app = new ImagemapSampleApp(lineMessagingClient, blobStorage, log);
//var app = new ImageCarouselSampleApp(lineMessagingClient, blobStorage, log);

var app = new RichMenuSampleApp(lineMessagingClient, log);

//var app = new RichMenuSampleApp(lineMessagingClient, log);
//var eventSourceLocation = await TableStorage<EventSourceLocation>.CreateAsync(connectionString, "eventsourcelocation");
//var app = new PostbackMessageSampleApp(lineMessagingClient, eventSourceLocation, log);

Expand All @@ -63,6 +61,11 @@ public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLeve
catch (Exception e)
{
log.Error(e.ToString());
var debugUserId = System.Configuration.ConfigurationManager.AppSettings["DebugUser"];
if (debugUserId != null)
{
await lineMessagingClient.PushMessageAsync(debugUserId, e.Message);
}
}

return req.CreateResponse(HttpStatusCode.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ by editing this MSBuild file. In order to learn more about this please visit htt
<PropertyGroup>
<WebPublishMethod>MSDeploy</WebPublishMethod>
<PublishProvider>AzureWebSite</PublishProvider>
<LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish>http://linebotfunctionsample.azurewebsites.net</SiteUrlToLaunchAfterPublish>
<LaunchSiteAfterPublish>False</LaunchSiteAfterPublish>
Expand Down
68 changes: 46 additions & 22 deletions FunctionAppSample/Samples/RichMenuSampleApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class RichMenuSampleApp : WebhookApplication

private TraceWriter Log { get; }

#region RichMenu definitions
private static readonly ImagemapSize _richMenuSize = ImagemapSize.RichMenuShort;
private static readonly int _buttonWidth = _richMenuSize.Width / 4;
private static readonly int _buttonHeight = _richMenuSize.Height;
Expand All @@ -26,31 +27,29 @@ class RichMenuSampleApp : WebhookApplication
private static readonly string MenuNameC = "RichMenuC";
private static readonly string MenuNameD = "RichMenuD";


private static readonly IList<ActionArea> _menuActionAreas = new[]
{
new ActionArea()
{
Bounds = new ImagemapArea(_buttonWidth * 0, 0, _buttonWidth, _buttonHeight),
Action = new PostbackTemplateAction("ButtonA", MenuNameA, "Change To Menu A")
Action = new PostbackTemplateAction("ButtonA", MenuNameA, "Menu A")
},
new ActionArea()
{
Bounds = new ImagemapArea(_buttonWidth * 1, 0, _buttonWidth, _buttonHeight),
Action = new PostbackTemplateAction("ButtonB", MenuNameB, "Change To Menu B")
Action = new PostbackTemplateAction("ButtonB", MenuNameB, "Menu B")
},
new ActionArea()
{
Bounds = new ImagemapArea(_buttonWidth * 2, 0, _buttonWidth, _buttonHeight),
Action = new PostbackTemplateAction("ButtonC", MenuNameC, "Change To Menu C")
Action = new PostbackTemplateAction("ButtonC", MenuNameC, "Menu C")
},
new ActionArea()
{
Bounds = new ImagemapArea(_buttonWidth * 3, 0, _buttonWidth, _buttonHeight),
Action = new PostbackTemplateAction("ButtonD", MenuNameD, "Change To Menu D")
Action = new PostbackTemplateAction("ButtonD", MenuNameD, "Menu D")
},
};
private List<ResponseRichMenu> _richMenus = new List<ResponseRichMenu>();

private static readonly RichMenu RichMenuA = new RichMenu
{
Expand Down Expand Up @@ -84,23 +83,25 @@ class RichMenuSampleApp : WebhookApplication
ChatBarText = "Menu D",
Areas = _menuActionAreas
};
#endregion

public RichMenuSampleApp(LineMessagingClient lineMessagingClient, TraceWriter log)
{
MessagingClient = lineMessagingClient;
Log = log;
}

public async Task CreateRichMenuAsync()
public async Task<IList<ResponseRichMenu>> CreateRichMenuAsync()
{
_richMenus.Clear();

var menuList = await MessagingClient.GetRichMenuList();
var menuList = await MessagingClient.GetRichMenuListAsync();
//await DeleteRichMenusAsync(menuList);

_richMenus.Add(await RegisterRichMenuAsync(RichMenuA));
_richMenus.Add(await RegisterRichMenuAsync(RichMenuB));
_richMenus.Add(await RegisterRichMenuAsync(RichMenuC));
_richMenus.Add(await RegisterRichMenuAsync(RichMenuD));
var newMenuList = new List<ResponseRichMenu>();
newMenuList.Add(await RegisterRichMenuAsync(RichMenuA));
newMenuList.Add(await RegisterRichMenuAsync(RichMenuB));
newMenuList.Add(await RegisterRichMenuAsync(RichMenuC));
newMenuList.Add(await RegisterRichMenuAsync(RichMenuD));
return newMenuList;

async Task<ResponseRichMenu> RegisterRichMenuAsync(RichMenu newItem)
{
Expand All @@ -111,12 +112,20 @@ async Task<ResponseRichMenu> RegisterRichMenuAsync(RichMenu newItem)
var image = CreateRichMenuImage(newItem);
await UploadRichMenuImageAsync(image, id);
item = newItem.ToResponseRichMenu(id);

}
return item;
}
}

private async Task DeleteRichMenusAsync(IList<ResponseRichMenu> menuList)
{
foreach (var menu in menuList)
{
await MessagingClient.DeleteRichMenuAsync(menu.RichMenuId);
}
menuList.Clear();
}

private async Task UploadRichMenuImageAsync(Image image, string richMenuId)
{
using (var stream = new MemoryStream())
Expand All @@ -133,6 +142,7 @@ private Image CreateRichMenuImage(RichMenu menu)
var g = Graphics.FromImage(bitmap);

var bkBrush = Brushes.White;

if (menu.Name == RichMenuA.Name)
{
bkBrush = Brushes.Red;
Expand All @@ -151,8 +161,8 @@ private Image CreateRichMenuImage(RichMenu menu)
}

g.FillRectangle(bkBrush, new Rectangle(0, 0, menu.Size.Width, menu.Size.Height));
using (var pen = new Pen(Color.DarkGray, 6.0f))
using (var font = new Font(FontFamily.GenericSansSerif, 40))
using (var pen = new Pen(Color.DarkGray, 10.0f))
using (var font = new Font(FontFamily.GenericSansSerif, 80))
{
foreach (var area in menu.Areas)
{
Expand All @@ -176,16 +186,30 @@ protected override async Task OnMessageAsync(MessageEvent ev)
{
Log.WriteInfo($"SourceType:{ev.Source.Type}, SourceId:{ev.Source.Id}, MessageType:{ev.Message.Type}");

await MessagingClient.LinkRichMenuToUserAsync(ev.Source.UserId, _richMenus[0].RichMenuId);
await MessagingClient.ReplyMessageAsync(ev.ReplyToken, "Open the Rich Menu!");
var textMessage = ev.Message as TextEventMessage;
if (textMessage?.Text?.StartsWith("Menu") ?? false)
{
return;
}

var memuList = await CreateRichMenuAsync();
var menuA = memuList.FirstOrDefault(m => m.Name == MenuNameA);
if (menuA == null) { return; }

await MessagingClient.LinkRichMenuToUserAsync(ev.Source.UserId, menuA.RichMenuId);
await MessagingClient.ReplyMessageAsync(ev.ReplyToken, "Hello Rich Menu!");
}

protected override async Task OnPostbackAsync(PostbackEvent ev)
{
var nextMenu = _richMenus.FirstOrDefault(menu => menu.Name == ev.Postback.Data);
var menuList = await MessagingClient.GetRichMenuListAsync();
var nextMenu = menuList.FirstOrDefault(menu => menu.Name == ev.Postback.Data);
if (nextMenu == null)
{
await MessagingClient.ReplyMessageAsync(ev.ReplyToken, $"Error!! {ev.Postback.Data} not found.");
}
await MessagingClient.LinkRichMenuToUserAsync(ev.Source.UserId, nextMenu.RichMenuId);
await MessagingClient.ReplyMessageAsync(ev.ReplyToken, $"Change the rich menu to {nextMenu.ChatBarText}");
await MessagingClient.ReplyMessageAsync(ev.ReplyToken, $"I changed a rich menu to {nextMenu.ChatBarText}");
}

}
}
Loading

0 comments on commit d5cad54

Please sign in to comment.