Skip to content

Commit

Permalink
Add support for Telegram.
Browse files Browse the repository at this point in the history
  • Loading branch information
ufukhalis committed Oct 14, 2022
1 parent 4c4fcce commit 5de1912
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 33 deletions.
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ Due to increasing demand, there is hard to find a slot immediately for your need
### How to use?

---
The application itself uses JVM to be run, you can download the JAR file from the releases section(good to have latest JRE installed in your env.) or clone it for some manual work :)
The application itself uses JVM to be run, you can download the JAR file from the [releases](https://github.com/ufukhalis/ind-appointment-checker/releases) section(good to have latest JRE installed in your env.) or clone it for some manual work :)

`IND Appointment Checker` is a console application so when you run the JAR file, you should pass some arguments to it. You can see available arguments running below command.

```shell
java -jar ind-appointment-checker-1.0.0.jar -h
java -jar ind-appointment-checker-1.1.0.jar -h

--messagingType, -t -> Messaging Type(whatsApp, telegram) (always required) { String }
--whatsAppApiKey, -wp-key -> WhatsApp Api Key { String }
Expand All @@ -32,17 +32,28 @@ XPAT_CENTER_EINDHOVEN, EXPAT_CENTER_DEN_HAAG, EXPAT_CENTER_ROTTERDAM, EXPAT_CENT
--help, -h -> Usage info
```
Before run the actual command, you need to prepare your phone from this link to be able to get messages -> https://www.callmebot.com/blog/free-api-whatsapp-messages/
> Currently, IND Appointment Checker application only supports WhatsApp, later on there will be other integration as well.
Before run the actual command, you need to prepare your phone from this link to be able to get messages, follow the below links.
* WhatsApp Integration -> https://www.callmebot.com/blog/free-api-whatsapp-messages/
* Telegram Integration -> https://www.callmebot.com/blog/telegram-text-messages/
> Currently, IND Appointment Checker application only supports WhatsApp and Telegram, later on there will be other integration as well.
And then, we can build the actual run command like below
```shell
java -jar ind-appointment-checker-1.0.0.jar -t whatsApp -wp-key {yourApiKey} -wp-pn {yourWhatsAppPhoneNumber} -fd 2022-12-30 -l AMSTERDAM -p 15 -
pd BIOMETRIC
# WhatsApp Usage
java -jar ind-appointment-checker-1.1.0.jar -t whatsApp -wp-key {yourApiKey} -wp-pn {yourWhatsAppPhoneNumber} -fd 2022-12-30 -l AMSTERDAM -p 15 -pd BIOMETRIC
```
Or
```shell
# Telegram Usage
java -jar ind-appointment-checker-1.1.0.jar -t telegram -tl-username {yourTelegramUserName} -fd 2022-12-30 -l AMSTERDAM -p 15 -pd BIOMETRIC
```
> The explanation of above command is that check a BIOMETRIC appointment in AMSTERDAM lower than this date 2022-10-30 for each 15 SECONDS and if there is one then send a message via WhatsApp.
> The explanation of above command is that check a BIOMETRIC appointment in AMSTERDAM lower than this date 2022-10-30 for each 15 SECONDS and if there is one then send a message via WhatsApp or Telegram(Depends on which type of messaging you choose).
> Once an appointment slot found, then the application will exit. So, you need to run it again to find new one.
Expand All @@ -55,4 +66,4 @@ Please do not abuse the IND system with given small period of time, put some rea
### What's next
---
There is a possibility that I may create a website version of this application so everybody that can reach easily.
There is a possibility that I may create a website version of this application so everybody that can reach easily.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "io.github.ufukhalis"
version = "1.0.0"
version = "1.1.0"

repositories {
mavenCentral()
Expand Down
12 changes: 8 additions & 4 deletions src/main/kotlin/io/github/ufukhalis/ind/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ fun main(args: Array<String>) {
shortName = "t",
description = "Messaging Type(whatsApp, telegram)"
).required()

val whatsAppApiKey by parser.option(ArgType.String, shortName = "wp-key", description = "WhatsApp Api Key")
val whatsAppPhoneNumber by parser.option(ArgType.String, shortName = "wp-pn", description = "WhatsApp Phone Number")

val telegramUserName by parser.option(ArgType.String, shortName = "tl-username", description = "Telegram User Name")

val filterDate by parser.option(ArgType.String, shortName = "fd", description = "Filter date").required()
val indLocationString by parser.option(
ArgType.String,
shortName = "l",
description = "IND Locations(${IndLocation.values().joinToString()})"
description = "IND Locations (${IndLocation.values().joinToString()})"
).required()
val indProductString by parser.option(
ArgType.String,
shortName = "pd",
description = "IND appointment types${IndProduct.values().joinToString()}"
description = "IND appointment types (${IndProduct.values().joinToString()})"
).required()
val period by parser.option(ArgType.Int, shortName = "p", description = "Checking period in seconds").default(30)

Expand Down Expand Up @@ -66,12 +69,13 @@ fun main(args: Array<String>) {
indLocation,
indProduct,
whatsAppPhoneNumber,
whatsAppApiKey
whatsAppApiKey,
telegramUserName
)
}

runBlocking {
indService.start(period.seconds).join()
}

}
}
5 changes: 3 additions & 2 deletions src/main/kotlin/io/github/ufukhalis/ind/service/IndService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ val indServiceModule = module {

val phoneNumber = params.get<String>(4)
val apiKey = params.get<String>(5)
val telegramUserName = params.get<String>(6)

val messageService = when (params.get<String>(0)) {
"telegram" -> inject<MessageService>(named("telegram"))
"telegram" -> inject<MessageService>(named("telegram")) { parametersOf(telegramUserName) }
"whatsApp" -> inject<MessageService>(named("whatsApp")) { parametersOf(phoneNumber, apiKey) }
else -> throw RuntimeException("Messaging service type is unknown!")
}.value
Expand All @@ -122,4 +123,4 @@ val indServiceModule = module {

IndService(httpClient, messageService, filterDate, indLocation, indProduct)
}
}
}
55 changes: 37 additions & 18 deletions src/main/kotlin/io/github/ufukhalis/ind/service/MessageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ interface MessageService {

suspend fun sendMessage(message: String): Either<Throwable, Int>

suspend fun encodeMessage(message: String): String {
return withContext(Dispatchers.IO) {
URLEncoder.encode(message, "UTF-8")
}
}
suspend fun sendRequest(httpClient: HttpClient, url: String, errorMessage: String): Either<Throwable, Int> {
return runCatching {
val httpResponse = httpClient.get(url)

when (val code = httpResponse.status.value) {
in 200..299 -> Either.Right(code)
else -> Either.Left(RuntimeException("$errorMessage -> $code"))
}
}.getOrElse {
Either.Left(it)
}
}
}

class WhatsAppMessageService(
Expand All @@ -24,26 +41,25 @@ class WhatsAppMessageService(
private val baseUrl = "https://api.callmebot.com/whatsapp.php?"

override suspend fun sendMessage(message: String): Either<Throwable, Int> {
return runCatching {
val encoded = withContext(Dispatchers.IO) {
URLEncoder.encode(message, "UTF-8")
}
val response = httpClient.get("${baseUrl}phone=${phoneNumber}&text=$encoded&apikey=${apiKey}")

when (val value = response.status.value) {
in 200..299 -> Either.Right(value)
else -> Either.Left(RuntimeException("The WhatsApp API has some issues -> $value"))
}
val encodedMessage = encodeMessage(message)
val url = "${baseUrl}phone=${phoneNumber}&text=$encodedMessage&apikey=${apiKey}"

}.getOrElse {
Either.Left(it)
}
return sendRequest(httpClient, url, "The WhatsApp API has some issues")
}
}

class TelegramMessageService : MessageService {
class TelegramMessageService(
private val httpClient: HttpClient,
private val telegramUserName: String
) : MessageService {

private val baseUrl = "https://api.callmebot.com/text.php?"
override suspend fun sendMessage(message: String): Either<Throwable, Int> {
TODO("Not yet implemented")
val encodedMessage = encodeMessage(message)

val url = "${baseUrl}user=@${telegramUserName}&text=${encodedMessage}"

return sendRequest(httpClient, url, "The Telegram API has some issues")
}
}

Expand All @@ -55,7 +71,10 @@ val messageServiceModule = module {
WhatsAppMessageService(httpClient, params[0], params[1])
}

single<MessageService>(named("telegram")) {
TelegramMessageService()
single<MessageService>(named("telegram")) { params ->

val httpClient: HttpClient by inject(named("httpClient"))

TelegramMessageService(httpClient, params[0])
}
}
}

0 comments on commit 5de1912

Please sign in to comment.