Skip to content
This repository was archived by the owner on Nov 11, 2022. It is now read-only.

Commit ab2b520

Browse files
authored
Merge pull request #19 from /issues/10
Fix #10: Simple monitoring
2 parents 034fac0 + 2faa840 commit ab2b520

File tree

46 files changed

+842
-358
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+842
-358
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.bat text eol=crlf
22

33
*.png filter=lfs diff=lfs merge=lfs -text
4+
*.jpeg filter=lfs diff=lfs merge=lfs -text
45
*.ttf filter=lfs diff=lfs merge=lfs -text

.github/workflows/deploy.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ jobs:
3939

4040
- run: sudo apt update && sudo apt install ansible
4141
- run: pip install oci
42-
- run: ansible-galaxy collection install community.crypto
43-
- run: ansible-galaxy collection install oracle.oci
4442
- run: ansible-galaxy install -r requirements.yml
4543
working-directory: deploy
4644

@@ -64,3 +62,10 @@ jobs:
6462
env:
6563
ANSIBLE_HOST_KEY_CHECKING: false
6664
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }}
65+
GRAFANA_CLOUD_PROMETHEUS_USER: ${{ secrets.GRAFANA_CLOUD_PROMETHEUS_USER }}
66+
GRAFANA_CLOUD_PROMETHEUS_PASSWORD: ${{ secrets.GRAFANA_CLOUD_PROMETHEUS_PASSWORD }}
67+
GRAFANA_CLOUD_LOKI_USER: ${{ secrets.GRAFANA_CLOUD_LOKI_USER }}
68+
GRAFANA_CLOUD_LOKI_PASSWORD: ${{ secrets.GRAFANA_CLOUD_LOKI_PASSWORD }}
69+
GRAFANA_CLOUD_GRAFANA_URL: ${{ secrets.GRAFANA_CLOUD_GRAFANA_URL }}
70+
GRAFANA_CLOUD_GRAFANA_API_KEY: ${{ secrets.GRAFANA_CLOUD_GRAFANA_API_KEY }}
71+
IMGUR_CLIENT_ID: ${{ secrets.IMGUR_CLIENT_ID }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ build
66
logo.png
77

88
templates/*.xcf
9+
10+
.env

README.adoc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
11
= https://t.me/ImgMacroBot[ImgMacroBot]
22

33
Telegram inline bot who helps you generate your favourite image macros on the fly.
4+
5+
== Running locally
6+
7+
. Define the required environment variables in `.env` file in the root of the project.
8+
The variables are:
9+
`TELEGRAM_TOKEN`::
10+
https://core.telegram.org/bots/api[Telegram Bot API] token.
11+
`TEMPLATES_DIR`::
12+
Absolute path to the link:templates[`templates`] directory of this project.
13+
`FONTS_DIR`::
14+
Absolute path to the link:fonts[`fonts`] directory of this project.
15+
`IMGUR_CLIENT_ID`::
16+
https://apidocs.imgur.com[Imgur API] client ID.
17+
`IMGMACROBOT_LOG_THRESHOLD`::
18+
Optional.
19+
Logging threshold: logs with levels below given won't be logged.
20+
`DEBUG` is a good value for development, `INFO` is used in production.
21+
`PORT`::
22+
Optional.
23+
Port to bind to.
24+
If no value given, `5000` will be used.
25+
26+
. Start https://ngrok.com[`ngrok`] with the given port, like:
27+
+
28+
[source,bash]
29+
----
30+
ngrok http 5000
31+
----
32+
33+
. Configure Telegram Bot API webhook with ngrok:
34+
+
35+
[source,bash]
36+
----
37+
curl --location --request POST 'https://api.telegram.org/bot${TELEGRAM_TOKEN}/setWebhook' \
38+
--header 'Content-Type: application/json' \
39+
--data-raw '{
40+
"url": "https://${NGROK}.ngrok.io/${TELEGRAM_TOKEN}"
41+
}
42+
'
43+
----
44+
+
45+
Don't forget to replace the placeholders.
46+
47+
. Start the bot:
48+
+
49+
[source,bash]
50+
----
51+
source .env && ./gradlew :runner:ktor:run
52+
----
53+
54+
. The bot is running!

buildSrc/src/main/kotlin/Versions.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ object Versions {
2424
const val KOTLINX_SERIALIZATION = "1.0.1"
2525
const val TGBOTAPI = "0.30.0"
2626
const val SKIJA = "0.89.18"
27+
const val MICROMETER = "1.6.4"
2728
const val LOG4J = "2.14.0"
29+
const val JACKSON = "2.12.1"
2830
const val JUNIT = "5.7.1"
2931
const val MOCKK = "1.10.6"
3032
}

core/build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ plugins {
44

55
dependencies {
66
implementation(platform("org.apache.logging.log4j:log4j-bom:${Versions.Dependencies.LOG4J}"))
7+
implementation(platform("io.micrometer:micrometer-bom:${Versions.Dependencies.MICROMETER}"))
78
testImplementation(platform("org.junit:junit-bom:${Versions.Dependencies.JUNIT}"))
89
testRuntimeOnly(platform("org.junit:junit-bom:${Versions.Dependencies.JUNIT}"))
910

10-
implementation(project(":imgur"))
11-
implementation("dev.inmo:tgbotapi:${Versions.Dependencies.TGBOTAPI}")
11+
api(project(":imgur"))
12+
api("io.micrometer:micrometer-core")
13+
api("dev.inmo:tgbotapi:${Versions.Dependencies.TGBOTAPI}")
1214
implementation("org.jetbrains.skija:skija-linux:${Versions.Dependencies.SKIJA}")
1315
implementation("org.apache.logging.log4j:log4j-core")
1416

core/src/main/kotlin/me/madhead/imgmacrobot/core/ImageMacroGenerationPipeline.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update
77
import kotlinx.coroutines.async
88
import kotlinx.coroutines.awaitAll
99
import kotlinx.coroutines.coroutineScope
10-
import kotlinx.coroutines.delay
11-
import kotlinx.coroutines.launch
12-
import kotlinx.coroutines.runBlocking
13-
import kotlinx.coroutines.supervisorScope
14-
import kotlinx.coroutines.time.delay
1510
import kotlinx.coroutines.withTimeout
16-
import kotlinx.coroutines.withTimeoutOrNull
1711
import org.apache.logging.log4j.LogManager
18-
import java.time.Duration
1912

2013
/**
2114
* [Telegram's Bot API updates](https://core.telegram.org/bots/api#getting-updates) processor. It responds with a list of generated image
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package me.madhead.imgmacrobot.core
2+
3+
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.abstracts.InlineQueryResult
4+
import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery
5+
import io.micrometer.core.instrument.MeterRegistry
6+
import io.micrometer.core.instrument.Timer
7+
8+
/**
9+
* A [ParsingImageMacroGenerator] implementation who tracks the timings of its delegates.
10+
*/
11+
class MeteredImageMacroGenerator<T : ParsedInlineQuery>(
12+
private val delegate: ParsingImageMacroGenerator<T>,
13+
private val registry: MeterRegistry,
14+
) : ParsingImageMacroGenerator<T> {
15+
private val timer = Timer
16+
.builder("imgmacrobot.generators")
17+
.description("Image macro generation stats")
18+
.tag("name", delegate::class.simpleName ?: "unknown")
19+
.register(registry)
20+
21+
override fun parseInlineQuery(inlineQuery: InlineQuery): T? = delegate.parseInlineQuery(inlineQuery)
22+
23+
override suspend fun generate(parsedInlineQuery: T): InlineQueryResult? {
24+
val sample = Timer.start(registry)
25+
26+
try {
27+
return delegate.generate(parsedInlineQuery)
28+
} finally {
29+
sample.stop(timer)
30+
}
31+
}
32+
}

core/src/main/kotlin/me/madhead/imgmacrobot/core/generators/IronicPalpatine.kt

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResult
44
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.abstracts.InlineQueryResult
55
import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery
66
import me.madhead.imgmacrobot.core.EmptyParsedInlineQuery
7-
import me.madhead.imgmacrobot.core.ImageMacroGenerationPipeline
8-
import me.madhead.imgmacrobot.core.ImageMacroGenerator
9-
import me.madhead.imgmacrobot.core.ParsedInlineQuery
107
import me.madhead.imgmacrobot.core.ParsingImageMacroGenerator
118
import me.madhead.imgmacrobot.imgur.ImageUploadRequest
12-
import me.madhead.imgmacrobot.imgur.ImageUploadResponseBodyDataSuccess
139
import me.madhead.imgmacrobot.imgur.Imgur
1410
import org.apache.logging.log4j.LogManager
1511
import java.nio.file.Path
@@ -46,21 +42,17 @@ class IronicPalpatine(
4642
image = file.readBytes(),
4743
name = "ironic.jpeg"
4844
))
49-
val data = upload.body.data
45+
val data = upload.data
5046

5147
logger.debug("Imgur upload result: {}", data)
5248

53-
if (data !is ImageUploadResponseBodyDataSuccess) {
54-
null
55-
} else {
56-
InlineQueryResultPhotoImpl(
57-
id = UUID.randomUUID().toString(),
58-
url = data.link,
59-
thumbUrl = data.link,
60-
width = data.width,
61-
height = data.height,
62-
)
63-
}
49+
InlineQueryResultPhotoImpl(
50+
id = UUID.randomUUID().toString(),
51+
url = data.link,
52+
thumbUrl = data.link,
53+
width = data.width,
54+
height = data.height,
55+
)
6456
}
6557
}
6658
}

core/src/main/kotlin/me/madhead/imgmacrobot/core/generators/WhatIfIToldYou.kt

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,27 @@ import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery
66
import me.madhead.imgmacrobot.core.ParsedInlineQuery
77
import me.madhead.imgmacrobot.core.ParsingImageMacroGenerator
88
import me.madhead.imgmacrobot.imgur.ImageUploadRequest
9-
import me.madhead.imgmacrobot.imgur.ImageUploadResponseBodyDataSuccess
109
import me.madhead.imgmacrobot.imgur.Imgur
1110
import org.apache.logging.log4j.LogManager
1211
import org.jetbrains.skija.Data
1312
import org.jetbrains.skija.EncodedImageFormat
1413
import org.jetbrains.skija.FontMgr
15-
import org.jetbrains.skija.FontStyle
1614
import org.jetbrains.skija.Image
1715
import org.jetbrains.skija.Surface
1816
import org.jetbrains.skija.Typeface
1917
import org.jetbrains.skija.paragraph.Alignment
2018
import org.jetbrains.skija.paragraph.FontCollection
21-
import org.jetbrains.skija.paragraph.HeightMode
2219
import org.jetbrains.skija.paragraph.ParagraphBuilder
2320
import org.jetbrains.skija.paragraph.ParagraphStyle
2421
import org.jetbrains.skija.paragraph.Shadow
25-
import org.jetbrains.skija.paragraph.StrutStyle
2622
import org.jetbrains.skija.paragraph.TextStyle
2723
import org.jetbrains.skija.paragraph.TypefaceFontProvider
24+
import java.io.File
2825
import java.nio.file.Path
2926
import java.util.UUID
3027
import kotlin.io.path.ExperimentalPathApi
31-
import kotlin.io.path.Path
3228
import kotlin.io.path.isRegularFile
3329
import kotlin.io.path.readBytes
34-
import kotlin.io.path.writeBytes
35-
import kotlin.math.abs
3630

3731
/**
3832
* Morpheus from the "The Matrix" trying to tell you something.
@@ -130,26 +124,23 @@ class WhatIfIToldYou(
130124
}
131125

132126
val snapshot = surface.makeImageSnapshot() ?: return null
133-
val data = snapshot.encodeToData(EncodedImageFormat.JPEG, 100) ?: return null
127+
val data = snapshot.encodeToData(EncodedImageFormat.JPEG, 95) ?: return null
128+
134129
val upload = imgur.imageUpload(ImageUploadRequest(
135130
image = data.bytes,
136131
name = "what if i told you.jpeg"
137132
))
138-
val responseData = upload.body.data
133+
val responseData = upload.data
139134

140135
logger.debug("Imgur upload result: {}", responseData)
141136

142-
return if (responseData !is ImageUploadResponseBodyDataSuccess) {
143-
null
144-
} else {
145-
InlineQueryResultPhotoImpl(
146-
id = UUID.randomUUID().toString(),
147-
url = responseData.link,
148-
thumbUrl = responseData.link,
149-
width = responseData.width,
150-
height = responseData.height,
151-
)
152-
}
137+
return InlineQueryResultPhotoImpl(
138+
id = UUID.randomUUID().toString(),
139+
url = responseData.link,
140+
thumbUrl = responseData.link,
141+
width = responseData.width,
142+
height = responseData.height,
143+
)
153144
}
154145
}
155146

0 commit comments

Comments
 (0)