Skip to content

Commit

Permalink
Merge pull request #19 from mi3lix9/change-docker-image
Browse files Browse the repository at this point in the history
Modify docker image to run the bot
  • Loading branch information
mi3lix9 committed Jun 20, 2024
2 parents b2f1f55 + 209f4a8 commit ca5ebdc
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 149 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@
node_modules/
jspm_packages/

# Develolpment directories
.devcontainer
.github
.vscode
out
# config file
config
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
BOT_TOKEN=123456789:COPYTHETOKENFROMBOTFATHER
JELLYSEERR_URL="https://localhost:5055/api/v1"
JELLYSEERR_URL="https://localhost:5055"
JELLYSEERR_KEY="COPYTHEAPIKEYFROMJELLYSEERR"
30 changes: 30 additions & 0 deletions .github/workflows/create-docker-image-by-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Build and Push to GHCR

on:
push:
branches:
- "develop"
- "main"

jobs:
build_and_push:
permissions:
packages: write
contents: read

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Build and Push the Image to GHCR
run: |
docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }}
if [ "${{ github.ref_name }}" == "main" ]; then
TAG="latest"
else
TAG="${{ github.ref_name }}"
fi
docker build -t ghcr.io/${{ github.repository }}:$TAG .
docker push ghcr.io/${{ github.repository }}:$TAG
61 changes: 0 additions & 61 deletions .github/workflows/create-docker-image-develop.yml

This file was deleted.

61 changes: 0 additions & 61 deletions .github/workflows/create-docker-image-latest.yml

This file was deleted.

Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM oven/bun:debian

# Config Bun
ENV PATH="~/.bun/bin:${PATH}"
RUN ln -s /usr/local/bin/bun /usr/local/bin/node
# RUN ln -s /usr/local/bin/bun /usr/local/bin/node


# Set working directory
Expand Down
4 changes: 3 additions & 1 deletion env.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { z } from "zod";

const envSchema = z.object({

BOT_TOKEN: z.string({ message: "BOT_TOKEN is not defined." }),
JELLYSEERR_URL: z.string({ message: "JELLYSEERR_URL is not defined." }).url(),
JELLYSEERR_URL: z.string({ message: "JELLYSEERR_URL is not defined." }).url().transform((url) => url + "/api/v1"),
JELLYSEERR_KEY: z.string({ message: "JELLYSEERR_KEY is not defined." }),
});

export default envSchema.parse(process.env);

21 changes: 6 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { Bot, session } from "grammy";
import env from "../env";
import { conversations, createConversation } from "@grammyjs/conversations";
import { SearchConversation } from "./conversations/search";
import type { MyContext } from "./utils/types";
import type { MyContext, SearchResult } from "./utils/types";
import { COMMANDS } from "./utils/commands";
import { fetchFromJellyseerr } from "./utils/fetchFromJellyseerr";
import { search } from "./jellyseerr";
import type { InlineQueryResultArticle } from "grammy/types";
import { createTemplate } from "./utils/createTemplate";
import { inlineQueryHandler } from "./utils/inlineQueryHandler";

const { BOT_TOKEN } = env;

const bot = new Bot<MyContext>(BOT_TOKEN);

bot.inlineQuery(/.*/, inlineQueryHandler);

// Install the session plugin.
bot.use(
session({
Expand Down Expand Up @@ -46,16 +50,3 @@ bot.catch(({ ctx, message }) => {

ctx.reply("Something went wrong.");
});

// const res = await search({ query: "one piece" });
// console.log(res);

// const json = await fetchFromJellyseerr("/request", {
// body: {
// mediaType: "movie",
// mediaId: 19576,
// },
// method: "POST",
// });

// console.log({ json });
18 changes: 10 additions & 8 deletions src/jellyseerr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ export async function search(input: SearchInput): Promise<SearchOutput> {
const results = json.results as SearchResult[];

return SearchOutput.parse(
results.map((r: SearchResult) => ({
mediaId: r.id,
title: r.title || r.name,
overview: r.overview,
releaseDate: r.releaseDate ?? r.firstAirDate,
posterPath: r.posterPath,
mediaType: r.mediaType,
}))
results
.filter((res) => res.mediaType !== "person")
.map((r: SearchResult) => ({
mediaId: r.id,
title: r.title || r.name,
overview: r.overview,
releaseDate: r.releaseDate ?? r.firstAirDate,
posterPath: r.posterPath,
mediaType: r.mediaType,
}))
);
}

Expand Down
39 changes: 39 additions & 0 deletions src/utils/inlineQueryHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { InlineQueryResultArticle } from "grammy/types";
import { search } from "../jellyseerr";
import { createTemplate } from "./createTemplate";
import type { MyContext } from "./types";
import type { Filter } from "grammy";

export async function inlineQueryHandler(
ctx: Filter<MyContext, "inline_query">
) {
const query = ctx.inlineQuery.query.trim();

if (query === "") {
return; // Ignore empty queries
}

const results = await search({ query });

// Format the results to send as inline query answers
const inlineResults: InlineQueryResultArticle[] = results
.filter((result) => typeof result.posterPath !== "undefined")
.map((result) => ({
type: "article",
id: String(result.mediaId),
title: result.mediaType + ": " + result.title,
input_message_content: {
message_text: createTemplate({
title: result.title,
overview: result.overview ?? "",
releaseDate: result.releaseDate ?? "",
mediaType: result.mediaType,
}),
},
description: result.overview,
thumb_url: result.posterPath,
}));

// Answer the inline query
await ctx.answerInlineQuery(inlineResults);
}
7 changes: 6 additions & 1 deletion src/zodSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ export const SearchOutput = z.array(
tags: z.array(z.string()).optional(),
posterPath: z
.string()
.or(z.null())
.optional()
.transform((val) => "https://image.tmdb.org/t/p/original" + val),
.transform((val) =>
typeof val === "string"
? "https://image.tmdb.org/t/p/original" + val
: undefined
),
mediaType: z.enum(["movie", "tv"]),
})
);
Expand Down

0 comments on commit ca5ebdc

Please sign in to comment.