Skip to content

Commit

Permalink
upgrade dotnet 6 and add about command (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrcn authored Oct 15, 2023
1 parent 54b75dc commit 6b68030
Show file tree
Hide file tree
Showing 32 changed files with 162 additions and 127 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy-lambda.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
- name: checkout
uses: actions/checkout@v2
- name: setup-dotnet
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: '3.1.x'
dotnet-version: '6.0.x'
- name: build
run: dotnet build
- name: install aws lambda tool
Expand Down
2 changes: 1 addition & 1 deletion aws-lambda-tools-defaults.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"region": "eu-west-1",
"configuration": "Release",
"framework": "netcoreapp2.1",
"framework": "dotnet6",
"stack-name": "KandilliEarthQuakeNotifierStack",
"s3-bucket": "kandilli-earthquake-notifier",
"s3-prefix": "builds/",
Expand Down
7 changes: 3 additions & 4 deletions src/Common/Common.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.17" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.17" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="S2Geometry" Version="1.0.3" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/Common/Exceptions/TelegramApiException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Common.Models;
using Newtonsoft.Json;
using System;
using System.Text.Json;

namespace Common.Exceptions
{
Expand All @@ -11,7 +11,7 @@ public class TelegramApiException : Exception
public TelegramApiException(string response)
:base(response)
{
Response = JsonConvert.DeserializeObject<TelegramResponse>(response);
Response = JsonSerializer.Deserialize<TelegramResponse>(response);
}
}
}
8 changes: 4 additions & 4 deletions src/Common/Models/AnswerCallbackQuery.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace Common.Models
{
public class AnswerCallbackQuery
{
[JsonProperty("callback_query_id")]
[JsonPropertyName("callback_query_id")]
public string CallbackQueryId { get; set; }

[JsonProperty("text")]
[JsonPropertyName("text")]
public string Text { get; set; }

[JsonProperty("show_alert")]
[JsonPropertyName("show_alert")]
public bool ShowAlert { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/Common/Models/TelegramDeleteMessage.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace Common.Models
{
public class TelegramDeleteMessage
{
[JsonProperty("chat_id")]
[JsonPropertyName("chat_id")]
public int ChatId { get; set; }

[JsonProperty("message_id")]
[JsonPropertyName("message_id")]
public int MessageId { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/Common/Models/TelegramInlineKeyboardButton.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace Common.Models
{
public class TelegramInlineKeyboardButton
{
[JsonProperty("text")]
[JsonPropertyName("text")]
public string Text { get; set; }

[JsonProperty("callback_data")]
[JsonPropertyName("callback_data")]
public string CallBackData { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/Common/Models/TelegramInlineKeyboardMarkup.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Common.Models
{
public class TelegramInlineKeyboardMarkup
{
[JsonProperty("inline_keyboard")]
[JsonPropertyName("inline_keyboard")]
public IEnumerable<IEnumerable<TelegramInlineKeyboardButton>> InlineKeyboard { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/Common/Models/TelegramKeyboardButton.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace Common.Models
{
public class TelegramKeyboardButton
{
[JsonProperty("text")]
[JsonPropertyName("text")]
public string Text { get; set; }

[JsonProperty("request_location")]
[JsonPropertyName("request_location")]
public bool RequestLocation { get; set; }
}
}
14 changes: 7 additions & 7 deletions src/Common/Models/TelegramMessage.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace Common.Models
{
public class TelegramMessage
{
[JsonProperty("chat_id")]
[JsonPropertyName("chat_id")]
public string ChatId { get; set; }

[JsonProperty("text")]
[JsonPropertyName("text")]
public string Text { get; set; }

[JsonProperty("parse_mode")]
[JsonPropertyName("parse_mode")]
public string ParseMode { get; set; }

[JsonProperty("disable_web_page_preview")]
[JsonPropertyName("disable_web_page_preview")]
public bool DisableWebPagePreview { get; set; }

[JsonProperty("disable_notification")]
[JsonPropertyName("disable_notification")]
public bool DisableNotification { get; set; }

[JsonProperty("reply_markup")]
[JsonPropertyName("reply_markup")]
public string ReplyMarkup { get; set; }
}
}
8 changes: 4 additions & 4 deletions src/Common/Models/TelegramReplyKeyboardMarkup.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Common.Models
{
public class TelegramReplyKeyboardMarkup
{
[JsonProperty("keyboard")]
[JsonPropertyName("keyboard")]
public IEnumerable<IEnumerable<TelegramKeyboardButton>> Keyboard { get; set; }

[JsonProperty("one_time_keyboard")]
[JsonPropertyName("one_time_keyboard")]
public bool OneTimeKeyboard { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/Common/Models/TelegramResponse.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace Common.Models
{
public class TelegramResponse
{
[JsonProperty("error_code")]
[JsonPropertyName("error_code")]
public int ErrorCode { get; set; }

[JsonProperty("description")]
[JsonPropertyName("description")]
public string Description { get; set; }
}
}
27 changes: 14 additions & 13 deletions src/Common/Services/TelegramService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Common.Exceptions;
using Common.Models;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Common.Services
Expand All @@ -17,22 +18,22 @@ public interface ITelegramService

public class TelegramService : ITelegramService
{
private const string TELEGRAM_API_URL = "https://api.telegram.org";
private const string TELEGRAM_API_TOKEN = "TELEGRAM_API_TOKEN";
private const string TelegramApiUrl = "https://api.telegram.org";
private const string TelegramApiToken = "TELEGRAM_API_TOKEN";

private readonly string _apiToken;

public TelegramService(IEnvironmentService environmentService)
{
_apiToken = environmentService.GetEnvironmentValue(TELEGRAM_API_TOKEN);
_apiToken = environmentService.GetEnvironmentValue(TelegramApiToken);
}

public async Task<bool> DeleteMessage(TelegramDeleteMessage telegramDeleteMessage)
{
var url = $"{TELEGRAM_API_URL}/bot{_apiToken}/deleteMessage";
var content = new StringContent(JsonConvert.SerializeObject(telegramDeleteMessage, new JsonSerializerSettings
var url = $"{TelegramApiUrl}/bot{_apiToken}/deleteMessage";
var content = new StringContent(JsonSerializer.Serialize(telegramDeleteMessage, new JsonSerializerOptions
{
NullValueHandling = NullValueHandling.Ignore
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
}), Encoding.UTF8, "application/json");

using (HttpClient client = new HttpClient())
Expand All @@ -42,10 +43,10 @@ public async Task<bool> DeleteMessage(TelegramDeleteMessage telegramDeleteMessag

public async Task<bool> SendMessage(TelegramMessage message)
{
var url = $"{TELEGRAM_API_URL}/bot{_apiToken}/sendMessage";
var content = new StringContent(JsonConvert.SerializeObject(message, new JsonSerializerSettings
var url = $"{TelegramApiUrl}/bot{_apiToken}/sendMessage";
var content = new StringContent(JsonSerializer.Serialize(message, new JsonSerializerOptions
{
NullValueHandling = NullValueHandling.Ignore
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
}), Encoding.UTF8, "application/json");

using (HttpClient client = new HttpClient())
Expand All @@ -61,10 +62,10 @@ public async Task<bool> SendMessage(TelegramMessage message)

public async Task<bool> AnswerCallbackQuery(AnswerCallbackQuery answerCallbackQuery)
{
var url = $"{TELEGRAM_API_URL}/bot{_apiToken}/answerCallbackQuery";
var content = new StringContent(JsonConvert.SerializeObject(answerCallbackQuery, new JsonSerializerSettings
var url = $"{TelegramApiUrl}/bot{_apiToken}/answerCallbackQuery";
var content = new StringContent(JsonSerializer.Serialize(answerCallbackQuery, new JsonSerializerOptions
{
NullValueHandling = NullValueHandling.Ignore
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
}), Encoding.UTF8, "application/json");

using (HttpClient client = new HttpClient())
Expand Down
6 changes: 5 additions & 1 deletion src/KandilliEarthquakeBot/Enums/BotDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
public static class BotDialog
{
public const string START_SUBSCRIPTION = "Deprem bildirimleri uyeliginiz basladi. Bildirimleri kisisellestirmek icin /siddet veya /konumekle komutlarini kullanabilirsiniz.";
public const string REMOVE_SUBSCRIPTION = "Deprem bildirimleri uyeliginiz silindi. Yeniden uye olmak icin /basla komutunu kullanabilirsiniz.";
public const string ASK_LOCATION = "Sadece 200km cevrenizdeki depremlerden haberdar olabilirsiniz, bunun icin deprem bildirimleri almak istediginiz dairesel bolgenin merkez konumunu paylasmaniz gerekiyor. Simdi Telegram'dan bu konumu paylasin.";
public const string REPLY_LOCATION = "Konum bilgisi kaydedildi, artik bu konumun 200km cevresindeki depremlerden haberdar olacaksiniz.";
public const string REMOVED_LOCATION = "Konum bilgisi kaldirildi.";
public const string ASK_MAGNITUDE = "En dusuk hangi siddetteki depremlerden haber almak istersiniz?";
public const string REPLY_MAGNITUDE = "Bildirimlerin deprem siddeti {0:f1} ve ustu olacak sekilde ayarlandi.";
public const string ABOUT = @"
Veriler Kandilli Rasathanesi'nden alinmaktadir. Verilerin dogrulugu garanti edilmez.
Begendiyseniz destek olmak icin; https://www.buymeacoffee.com/mete
";
}
}
3 changes: 2 additions & 1 deletion src/KandilliEarthquakeBot/Enums/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum Command
Magnitude = 200,
Location = 300,
RemoveLocation = 301,
Stop = 400
Stop = 400,
About = 500
}
}
7 changes: 5 additions & 2 deletions src/KandilliEarthquakeBot/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
using KandilliEarthquakeBot.Models;
using KandilliEarthquakeBot.Services;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
Expand All @@ -31,7 +31,7 @@ public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyReques

try
{
webhookMessage = JsonConvert.DeserializeObject<WebhookMessage>(request.Body);
webhookMessage = JsonSerializer.Deserialize<WebhookMessage>(request.Body);
}
catch(Exception ex)
{
Expand Down Expand Up @@ -69,6 +69,9 @@ public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyReques
case Command.RemoveLocation:
await botService.RemoveLocationAsync(webhookMessage.Message.Chat.Id);
break;
case Command.About:
await botService.About(webhookMessage.Message.Chat.Id);
break;
}
}
catch (TelegramApiException exception) when (exception.Response.ErrorCode == 403) //bot blocked, delete subscription
Expand Down
5 changes: 4 additions & 1 deletion src/KandilliEarthquakeBot/Helpers/CommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class CommandHelper
{
public static bool TryParseCommand(string chatMessage, out Command command)
{
string pattern = @"\/(basla|start|siddet|konumekle|konumsil|bitir|stop)";
string pattern = @"\/(basla|start|siddet|konumekle|konumsil|bitir|stop|hakkinda)";
command = Command.None;
var result = Regex.Match(chatMessage, pattern);

Expand All @@ -33,6 +33,9 @@ public static bool TryParseCommand(string chatMessage, out Command command)
case "konumsil":
command = Command.RemoveLocation;
break;
case "hakkinda":
command = Command.About;
break;
}
return true;
}
Expand Down
14 changes: 7 additions & 7 deletions src/KandilliEarthquakeBot/KandilliEarthquakeBot.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.4.0" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.0.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.0.0" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.0.50" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.4" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.6.0" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.1.1" />
<PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.203.2" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.7" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
Expand Down
9 changes: 8 additions & 1 deletion src/KandilliEarthquakeBot/Models/CallbackQuery.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
namespace KandilliEarthquakeBot.Models
using System.Text.Json.Serialization;

namespace KandilliEarthquakeBot.Models
{
public class CallbackQuery
{
[JsonPropertyName("id")]
public string Id { get; set; }

[JsonPropertyName("message")]
public Message Message { get; set; }

[JsonPropertyName("data")]
public string Data { get; set; }
}
}
Loading

0 comments on commit 6b68030

Please sign in to comment.