public async Task <bool> Install(ILineEvent e, string moduleName, string moduleZipPath) { if (!SuperUsers.Contains((e.Source as IUser).Id)) { await Bot.Reply(e.ReplyToken, new TextMessage($"無權限執行此命令")); return(true); } var client = new HttpClient(); var temp = Path.GetTempFileName(); using (var fileStream = File.Create(temp)) { (await client.GetStreamAsync(moduleZipPath)).CopyTo(fileStream); } using (var fileStream = File.Open(temp, FileMode.Open)) using (ZipArchive arch = new ZipArchive(fileStream, ZipArchiveMode.Read, true)) { arch.ExtractToDirectory(Path.Combine(LineBotService.ContentRootPath, "plugins", moduleName)); } File.Delete(temp); await Bot.Reply(e.ReplyToken, new TextMessage($"已安裝,請重啟服務")); return(true); }
public async Task Group_MessageTypeIsImage_ReturnsMessage() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.Messages.Image); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); IEventSource source = lineEvent.Source; Assert.IsNotNull(source); Assert.AreEqual(EventSourceType.User, source.SourceType); Assert.AreEqual("U206d25c2ea6bd87c17655609a1c37cb8", source.User.Id); Assert.AreEqual("nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", lineEvent.ReplyToken); Assert.IsNotNull(lineEvent.Message); Assert.AreEqual("325708", lineEvent.Message.Id); Assert.IsNull(lineEvent.Message.Location); Assert.AreEqual(MessageType.Image, lineEvent.Message.MessageType); Assert.AreEqual("nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", lineEvent.Message.ReplyToken); Assert.IsNull(lineEvent.Message.Sticker); Assert.IsNull(lineEvent.Message.Text); }
public async Task GetEvents_ValidRequest_ReturnsBeaconEvent() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(BeaconEventJson); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.IsNotNull(events); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); Assert.AreEqual(LineEventType.Beacon, lineEvent.EventType); IEventSource source = lineEvent.Source; Assert.IsNotNull(source); Assert.AreEqual(EventSourceType.User, source.SourceType); Assert.AreEqual("U206d25c2ea6bd87c17655609a1c37cb8", source.User.Id); Assert.AreEqual("nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", lineEvent.ReplyToken); IBeacon beacon = lineEvent.Beacon; Assert.IsNotNull(beacon); Assert.AreEqual("d41d8cd98f", beacon.Hwid); Assert.AreEqual(BeaconType.Enter, beacon.BeaconType); Assert.AreEqual("nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", beacon.ReplyToken); }
public async Task Group_MessageTypeIsLocation_ReturnsMessage() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.Messages.Location); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); IEventSource source = lineEvent.Source; Assert.IsNotNull(source); Assert.AreEqual(EventSourceType.User, source.SourceType); Assert.AreEqual("U206d25c2ea6bd87c17655609a1c37cb8", source.User.Id); Assert.AreEqual("nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", lineEvent.ReplyToken); Assert.IsNotNull(lineEvent.Message); Assert.AreEqual("325708", lineEvent.Message.Id); Assert.AreEqual(MessageType.Location, lineEvent.Message.MessageType); Assert.AreEqual("nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", lineEvent.Message.ReplyToken); Assert.IsNull(lineEvent.Message.Text); Assert.IsNull(lineEvent.Message.Sticker); ILocation location = lineEvent.Message.Location; Assert.IsNotNull(location); Assert.AreEqual("〒150-0002 東京都渋谷区渋谷2丁目21−1", location.Address); Assert.AreEqual(35.65910807942215m, location.Latitude); Assert.AreEqual(139.70372892916203m, location.Longitude); Assert.AreEqual("my location", location.Title); }
public async Task <bool> Out(ILineEvent e) { if (!SuperUsers.Contains((e.Source as IUser).Id)) { await Bot.Reply(e.ReplyToken, new TextMessage($"無權限執行此命令")); return(true); } await Bot.Reply(e.ReplyToken, new TextMessage($"BYE")); switch (e.Source.SourceType) { case EventSourceType.Group: await Bot.LeaveGroup(e.Source.Group); break; case EventSourceType.Room: await Bot.LeaveRoom(e.Source.Room); break; } return(true); }
public async Task <bool> Handle(ILineEvent e) { Regex check = new Regex(@"(今天|現在|當前|當下)?(?<City>.+)天氣((如何|怎樣|狀況|情形)\??)?$"); if (!check.IsMatch(e.Message.Text ?? "")) { return(false); } var cityname = check.Match(e.Message.Text).Groups["City"].Value; cityname = cityname.Replace("臺", "台").Replace("縣", "").Replace("市", ""); if (!WeatherConfig.CityCode.ContainsKey(cityname)) { await Bot.Reply(e.ReplyToken, new TextMessage("找不到您說的地區:" + cityname)); return(true); } var citycode = WeatherConfig.CityCode[cityname]; var weatherInfo = JObject.Parse(await client.GetStringAsync($"https://works.ioa.tw/weather/api/weathers/{citycode}.json")); await Bot.Reply(e.ReplyToken, new TextMessage( $"{cityname}地區目前天氣:【{weatherInfo["desc"].Value<string>()}】\r\n" + $"溫度:{weatherInfo["temperature"].Value<string>()}度(體感溫度:{weatherInfo["felt_air_temp"].Value<string>()})\r\n" + $"濕度:{weatherInfo["humidity"].Value<string>()}%\r\n" + $"降雨量:{weatherInfo["rainfall"].Value<string>()}mm" )); return(true); }
public async Task <bool> Handle(ILineEvent e) { if (e.EventType != LineEventType.Message) { return(false); } string message = e.Message?.Text?.Trim(); if (message == null || !message.StartsWith("linebot ")) { return(false); } var parameters = message.Split(' ').Skip(1).ToArray(); var targetMethod = GetType().GetMethods().FirstOrDefault(x => x.Name.ToLower() == parameters[0]); if (targetMethod == null || targetMethod.Name == "Handle") { await Bot.Reply(e.ReplyToken, new TextMessage($"無效的指令")); return(true); } Console.WriteLine(parameters); return(await((Task <bool>)targetMethod.Invoke(this, new object[] { e }.Concat(parameters.Skip(1)).ToArray()))); }
public async Task <bool> Disable(ILineEvent e, string moduleName) { if (!SuperUsers.Contains((e.Source as IUser).Id)) { await Bot.Reply(e.ReplyToken, new TextMessage($"無權限執行此命令")); return(true); } var moduleIndex = LineBotService.ReplyModules.FindIndex(x => x.type.Name.ToLower() == moduleName.ToLower()); LineBotService.ReplyModules[moduleIndex] = (LineBotService.ReplyModules[moduleIndex].type, false); await Bot.Reply(e.ReplyToken, new TextMessage($"成功停用模組: {moduleName}")); string[] moduleNames = new string[0]; using (var file = File.Open("disableModules.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) { moduleNames = new StreamReader(file).ReadToEnd()?.Split(',') ?? new string[0]; moduleNames = moduleNames.Concat(new string[] { moduleName }).Select(x => x.Trim()).Distinct().ToArray(); } File.Delete("disableModules.txt"); File.WriteAllText("disableModules.txt", string.Join(",", moduleNames)); return(true); }
public async Task GetEvents_ValidRequest_ReturnsPostbackEvent() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.Postback); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.IsNotNull(events); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); Assert.AreEqual(LineEventType.Postback, lineEvent.EventType); IEventSource source = lineEvent.Source; Assert.IsNotNull(source); Assert.AreEqual(EventSourceType.User, source.SourceType); Assert.AreEqual("U206d25c2ea6bd87c17655609a1c37cb8", source.User.Id); Assert.AreEqual("nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", lineEvent.ReplyToken); IPostback postback = lineEvent.Postback; Assert.IsNotNull(postback); Assert.AreEqual("action=buyItem&itemId=123123&color=red", postback.Data); Assert.AreEqual("nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", postback.ReplyToken); }
public async Task <bool> Whoami(ILineEvent e) { await Bot.Reply(e.ReplyToken, new TextMessage( e.Source.Group?.Id ?? e.Source.Room?.Id ?? e.Source.User?.Id )); return(true); }
public async Task <bool> Help(ILineEvent e) { await Bot.Reply(e.ReplyToken, new TextMessage( $"您可以執行以下命令: out whoami install uninstall enable disable list login logout stop" )); return(true); }
public async Task GetEvents_InvalidRequest_ReplyTokenReturnsNull() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.Invalid); IEnumerable<ILineEvent> events = await bot.GetEvents(request); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); Assert.IsNull(lineEvent.ReplyToken); }
public async Task <bool> Handle(ILineEvent e) { if (!e.Message.Text?.StartsWith("echo:", StringComparison.CurrentCultureIgnoreCase) ?? true) { return(false); } var message = e.Message.Text.Substring(5); await Bot.Reply(e.ReplyToken, new TextMessage(message)); return(true); }
public async Task GetEvents_InvalidRequest_BeaconIsNull() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(InvalidJson); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); Assert.IsNull(lineEvent.Beacon); }
public async Task <bool> Logout(ILineEvent e) { if (SuperUsers.Contains((e.Source as IUser).Id)) { SuperUsers.Remove((e.Source as IUser).Id); } var userProfile = await Bot.GetProfile((e.Source as IUser).Id); await Bot.Reply(e.ReplyToken, new TextMessage($"{userProfile.DisplayName} 已經登出為一般使用者")); return(true); }
/// <summary> /// /// </summary> /// <param name="lineBot"></param> /// <param name="lineEvent"></param> /// <returns></returns> public async Task Handle(ILineBot lineBot, ILineEvent lineEvent) { if (lineEvent.Beacon.BeaconType == BeaconType.Unknown) { return; } var eventSourceId = EventSource.GetEventSourceId(lineEvent); if (string.IsNullOrEmpty(eventSourceId)) { return; } }
public async Task GetEvents_InvalidMessageType_MessageTypeIsUnknown() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.Messages.Invalid); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); Assert.IsNotNull(lineEvent.Message); Assert.AreEqual(MessageType.Unknown, lineEvent.Message.MessageType); }
public async Task <bool> List(ILineEvent e) { if (!SuperUsers.Contains((e.Source as IUser).Id)) { await Bot.Reply(e.ReplyToken, new TextMessage($"無權限執行此命令")); return(true); } var modulesList = string.Join("\r\n", LineBotService.ReplyModules.Select(x => x.type.Name + (x.enable ? "" : "(停用)"))); await Bot.Reply(e.ReplyToken, new TextMessage($"目前已經安裝以下模組:\r\n{modulesList}")); return(true); }
public async Task <bool> Stop(ILineEvent e) { if (!SuperUsers.Contains((e.Source as IUser).Id)) { await Bot.Reply(e.ReplyToken, new TextMessage($"無權限執行此命令")); return(true); } await Bot.Reply(e.ReplyToken, new TextMessage($"正在關閉聊天機器人服務")); Environment.Exit(0); return(true); }
public async Task GetEvents_RequestWithoutBeacon_BeaconIsNull() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(BeaconEventWithoutBeaconJson); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.IsNotNull(events); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); Assert.AreEqual(LineEventType.Beacon, lineEvent.EventType); Assert.IsNull(lineEvent.Beacon); }
public async Task GetEvents_RequestWithoutPostback_PostbackIsNull() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.WithoutPostback); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.IsNotNull(events); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); Assert.AreEqual(LineEventType.Postback, lineEvent.EventType); Assert.IsNull(lineEvent.Postback); }
public async Task Handle(ILineBot lineBot, ILineEvent evt) { if (string.IsNullOrEmpty(evt.Message.Text)) { return; } // The Webhook URL verification uses these invalid token. if (evt.ReplyToken == "00000000000000000000000000000000" || evt.ReplyToken == "ffffffffffffffffffffffffffffffff") { return; } if (evt.Message.Text.ToLowerInvariant().Contains("ping")) { var response = new TextMessage($"pong"); await lineBot.Reply(evt.ReplyToken, response); } else if (evt.Message.Text.ToLowerInvariant().Contains("user")) { var userName = evt.Source.User.Id; try { var user = await lineBot.GetProfile(evt.Source.User); userName = $"{user.DisplayName} ({user.UserId})"; } catch (LineBotException) { } var response = new TextMessage($"You are: {userName}"); await lineBot.Reply(evt.ReplyToken, response); } else if (evt.Message.Text.ToLowerInvariant().Contains("logo")) { var logoUrl = this.configuration.ResourcesUrl + "/Images/Line.Bot.SDK.png"; Console.WriteLine(logoUrl); var response = new ImageMessage(logoUrl, logoUrl); await lineBot.Reply(evt.ReplyToken, response); } }
public async Task <bool> Uninstall(ILineEvent e, string moduleName) { if (!SuperUsers.Contains((e.Source as IUser).Id)) { await Bot.Reply(e.ReplyToken, new TextMessage($"無權限執行此命令")); return(true); } try { Directory.Delete(Path.Combine(LineBotService.ContentRootPath, "plugins", moduleName), true); await Bot.Reply(e.ReplyToken, new TextMessage($"已解除安裝,請重啟服務")); } catch { await Bot.Reply(e.ReplyToken, new TextMessage($"解除安裝失敗,請關閉服務後手動刪除目錄")); } return(true); }
public async Task <bool> Handle(ILineEvent e) { if (e.EventType != LineEventType.Message) { return(false); } if (e.Message?.Text?.IndexOf("echo:") != 0) { return(false); } var context = e.Message.Text.Split(':').Last(); await Bot.Reply(e.ReplyToken, new TextMessage($"收到指令:{context}")); return(true); }
public async Task Handle(ILineBot lineBot, ILineEvent evt) { string userName = "******"; try { var user = await lineBot.GetProfile(evt.Source.User); userName = $"{user.DisplayName} ({user.UserId})"; } catch (LineBotException) { } var response = new TextMessage($"Welcome, {userName} !"); await lineBot.Reply(evt.ReplyToken, response); }
/// <summary> /// Gets the event source identifier. /// </summary> /// <returns>The event source identifier.</returns> /// <param name="lineEvent">Line event.</param> public static string GetEventSourceId(ILineEvent lineEvent) { var result = string.Empty; switch (lineEvent.Source.SourceType) { case EventSourceType.User: result = lineEvent.Source.User.Id; break; case EventSourceType.Group: result = lineEvent.Source.Group.Id; break; case EventSourceType.Room: result = lineEvent.Source.Room.Id; break; } return(result); }
public async Task GetEvents_ValidRequest_IsLeaveEvent() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(LeaveEventJson); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.IsNotNull(events); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); Assert.AreEqual(LineEventType.Leave, lineEvent.EventType); IEventSource source = lineEvent.Source; Assert.IsNotNull(source); Assert.AreEqual(EventSourceType.Group, source.SourceType); Assert.AreEqual("cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", source.Group.Id); }
public async Task GetEvents_ValidRequest_IsUnfollowEvent() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.Unfollow); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.IsNotNull(events); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); Assert.AreEqual(LineEventType.Unfollow, lineEvent.EventType); IEventSource source = lineEvent.Source; Assert.IsNotNull(source); Assert.AreEqual(EventSourceType.User, source.SourceType); Assert.AreEqual("U206d25c2ea6bd87c17655609a1c37cb8", source.User.Id); }
public async Task GetEvents_ValidRequest_ReturnsFollowEvent() { ILineBot bot = TestConfiguration.CreateBot(); TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.Join); IEnumerable <ILineEvent> events = await bot.GetEvents(request); Assert.IsNotNull(events); Assert.AreEqual(1, events.Count()); ILineEvent lineEvent = events.First(); Assert.AreEqual(LineEventType.Join, lineEvent.EventType); IEventSource source = lineEvent.Source; Assert.IsNotNull(source); Assert.AreEqual(EventSourceType.Group, source.SourceType); Assert.AreEqual("cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", source.Group.Id); Assert.AreEqual("nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", lineEvent.ReplyToken); }
public async Task <bool> Login(ILineEvent e, string channelSecret) { if (channelSecret != Configuration.ChannelSecret) { await Bot.Reply(e.ReplyToken, new TextMessage($"登入失敗,ChannelSecret錯誤")); return(true); } Console.WriteLine(SuperUsers == null); Console.WriteLine(JsonConvert.SerializeObject(e.Source, Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }).ToString()); SuperUsers.Add((e.Source as IUser).Id); var userProfile = await Bot.GetProfile((e.Source as IUser).Id); await Bot.Reply(e.ReplyToken, new TextMessage($"{userProfile.DisplayName} 已經登入為超級使用者")); return(true); }