Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into node-bot
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedegeofroy authored Aug 18, 2024
2 parents bec98e5 + faf9a39 commit 482e609
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
develop="true"
PORT="3500"
bot_token="token"
bot_username="dishadsh_bot"
api_url="https://dishdash.ru"
frontend_url="https://bot.dishdash.ru"
bot_url="bot_url"
host_url="bot_url_without_https"
host_url="bot_url_without_https"
report_chat_id="report_chat_id"
49 changes: 49 additions & 0 deletions .github/workflows/cd-build-push-deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Publish Docker image

on:
workflow_run:
workflows: ["CI"]
types:
- completed

jobs:
push_to_registry:
name: Push Docker image to Docker Hub
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'main' }}
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
attestations: write
id-token: write
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set short git commit SHA
id: vars
run: |
calculatedSha=$(git rev-parse --short ${{ github.sha }})
echo "COMMIT_SHORT_SHA=$calculatedSha" >> $GITHUB_ENV
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push Docker image
id: push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: |
shampiniony/dishdash-bot:latest
shampiniony/dishdash-bot:${{ env.COMMIT_SHORT_SHA }}
- name: Trigger watchtower to update container(s)
shell: bash
run: |
curl -H "Authorization: Bearer ${{ secrets.WATCHTOWER_HTTP_API_TOKEN }}" ${{ secrets.WATCHTOWER_UPDATE_ENDPOINT }}
22 changes: 22 additions & 0 deletions .github/workflows/ci-tests-build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI

on:
push:
branches: [ "main", "dev" ]
pull_request:
branches: [ "main", "dev" ]

jobs:

build:

runs-on: ubuntu-22.04

steps:
- name: Set short git commit SHA
run: |
echo "CI test complited"
# - uses: actions/checkout@v4
# - name: Build docker image
# run: docker-compose -f docker-compose.yaml build
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ RUN npm install

COPY . .

COPY .env .env

CMD ["npm", "start"]
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Bot

## How to use image?

Example `docker-compose.yaml`
```
version: '3'
services:
dishdash-bot:
image: shampiniony/dishdash-bot:latest
environment:
- API_URL=${API_URL}
- BOT_TOKEN=${BOT_TOKEN}
- BOT_URL=${BOT_URL}
- BOT_USERNAME=${BOT_USERNAME}
- DEVELOP=${DEVELOP}
- FRONTEND_URL=${FRONTEND_URL}
- HOST_URL=${HOST_URL}
- PORT=${PORT}
```
File renamed without changes.
52 changes: 45 additions & 7 deletions routes/dashBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const host = `dash`;

const token = process.env.bot_token;
const bot_username = process.env.bot_username;
const report_chat_id = process.env.report_chat_id; // ID чата для пересылки сообщений

var express = require('express');
var router = express.Router();
Expand Down Expand Up @@ -62,27 +63,62 @@ router.get(`/avatar`, (req, res) => {
});

router.post(`/hook`, (req, res) => {
res.sendStatus(200);
let sessionStates = {}; // Объект для хранения состояния сессий
let reportChatId = report_chat_id;

res.sendStatus(200);
devlog(JSON.stringify(req.body, null, 2));

if (req.body.message) {
let message = req.body.message;
let chat_id = message.chat.id;

if (!sessionStates[chat_id]) {
sessionStates[chat_id] = { reportMode: false };
}

let currentSession = sessionStates[chat_id];

if (message.text) {
let text = message.text;

if (text === '/start') {
if (currentSession.reportMode) {
currentSession.reportMode = false;

if (report_chat_id === undefined) {
console.log("`report_chat_id` is undefined. Check envs to turn on reports.");
} else {
let reportTime = new Date().toLocaleString();

sendMessage(
{
chat_id: reportChatId,
text: `⚠️ <b>Репорт от пользователя:</b>\n\n<b>Имя:</b> ${message.from.first_name} ${message.from.last_name || ''}\n<b>Юзернейм:</b> @${message.from.username || 'нет юзернейма'}\n<b>Chat ID:</b> ${chat_id}\n<b>Время обращения:</b> ${reportTime}\n\n<b>Сообщение:</b>\n${text}`,
parse_mode: 'HTML',
},
'sendMessage',
token
);

sendMessage(
{
chat_id: chat_id,
text: 'Ваш репорт был успешно отправлен администрации.',
},
'sendMessage',
token
);
}
} else if (text === '/start') {
let fullName = message.from.first_name;
if (message.from.last_name) {
fullName += ` ${message.from.last_name}`;
}

sendMessage(
{
chat_id: chat_id,
text: `Добро пожаловать, <b>${fullName}</b>!\n\nИспользуйте DishDash, чтобы легко и быстро выбрать место для встречи в компании. Упомяните бота в чате и введите <code>@dishdash_bot start</code> для создания лобби.\n\nБот <a href='https://dishdash.ru'>DishDash</a> разработан командой <a href='https://t.me/+4l8DChDSxMQxNWUy'>\"Шампиньоны\"</a>`,
text: `Добро пожаловать, <b>${fullName}</b>!\n\nИспользуйте DishDash, чтобы легко и быстро выбрать место для встречи в компании. Упомяните бота в чате и введите <code>@dishdash_bot start</code> для создания лобби.\n\nБот <a href='https://dishdash.ru'>DishDash</a> разработан командой <a href='https://t.me/+4l8DChDSxMQxNWUy'>\"Шампиньоны\"</a>.`,
parse_mode: 'HTML',
},
'sendMessage',
Expand All @@ -92,16 +128,17 @@ router.post(`/hook`, (req, res) => {
sendMessage(
{
chat_id: chat_id,
text: 'Доступные команды:\n/start - Запуск бота\n/report — Баг репорт\n/help - Справка',
text: 'Доступные команды:\n/start - Запуск бота\n/help - Справка\n/report - сообщить о баге/проблеме',
},
'sendMessage',
token
);
} else if (text === '/report') {
currentSession.reportMode = true;
sendMessage(
{
chat_id: chat_id,
text: 'Извините, я не понимаю эту команду.',
text: 'Теперь отправьте сообщение, которое вы хотите сообщить администрации.',
},
'sendMessage',
token
Expand All @@ -117,7 +154,8 @@ router.post(`/hook`, (req, res) => {
);
}
}
}
}


if (req.body.inline_query) {
let q = req.body.inline_query;
Expand Down

0 comments on commit 482e609

Please sign in to comment.