Compare commits
40 Commits
5f151d9579
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| a767ab39d7 | |||
| c5ff370f75 | |||
| 59770b1b0f | |||
|
|
26519dd977 | ||
|
|
4b67bf10d3 | ||
|
|
0ebb75de64 | ||
|
|
14592c2b8a | ||
| f036f521d8 | |||
| 699eafd0df | |||
| 7337a24126 | |||
| 5b9c1a18b7 | |||
| 4b63faabef | |||
| 44d4758688 | |||
| 9514554d48 | |||
| eadc1be6d3 | |||
| 510dc9b8f7 | |||
| 0046701dbb | |||
| e7109cc254 | |||
| dc7c222f50 | |||
| 2d051a1881 | |||
| e6e3762768 | |||
| 8a4a6ac217 | |||
| 3d379bc6d7 | |||
| 443b943bee | |||
| 1d543461e9 | |||
| 37ac151e68 | |||
| 7f947f3fa0 | |||
| 53e929fe5a | |||
| e150d910d4 | |||
| 118195a05f | |||
| a92977723e | |||
| 00b891f8fd | |||
| 11d4262890 | |||
| 7b77ca2f56 | |||
| 9e472f03a4 | |||
| 41e1629892 | |||
| 0fcc94d81b | |||
| 9add6da6e4 | |||
| f327366eaf | |||
| ab52295e6e |
@@ -2,33 +2,17 @@ version: "3.4"
|
|||||||
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
worker:
|
||||||
bot:
|
|
||||||
image: mathwave/sprint-repo:pizda-bot
|
image: mathwave/sprint-repo:pizda-bot
|
||||||
command: bot
|
command: worker
|
||||||
environment:
|
environment:
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
MONGO_HOST: "mongo"
|
||||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
STAGE: "development"
|
|
||||||
deploy:
|
|
||||||
mode: replicated
|
|
||||||
restart_policy:
|
|
||||||
condition: any
|
|
||||||
update_config:
|
|
||||||
parallelism: 1
|
|
||||||
order: start-first
|
|
||||||
|
|
||||||
updater:
|
|
||||||
image: mathwave/sprint-repo:pizda-bot
|
|
||||||
command: updater
|
|
||||||
environment:
|
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
|
||||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
|
networks:
|
||||||
|
- queues-development
|
||||||
|
- configurator
|
||||||
|
- mongo-development
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -41,13 +25,12 @@ services:
|
|||||||
image: mathwave/sprint-repo:pizda-bot
|
image: mathwave/sprint-repo:pizda-bot
|
||||||
command: api
|
command: api
|
||||||
environment:
|
environment:
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
MONGO_HOST: "mongo"
|
||||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
networks:
|
networks:
|
||||||
- common-infra-nginx
|
- common-infra-nginx-development
|
||||||
|
- mongo-development
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -57,5 +40,11 @@ services:
|
|||||||
order: start-first
|
order: start-first
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
common-infra-nginx:
|
common-infra-nginx-development:
|
||||||
|
external: true
|
||||||
|
queues-development:
|
||||||
|
external: true
|
||||||
|
configurator:
|
||||||
|
external: true
|
||||||
|
mongo-development:
|
||||||
external: true
|
external: true
|
||||||
@@ -2,33 +2,17 @@ version: "3.4"
|
|||||||
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
worker:
|
||||||
bot:
|
|
||||||
image: mathwave/sprint-repo:pizda-bot
|
image: mathwave/sprint-repo:pizda-bot
|
||||||
command: bot
|
command: worker
|
||||||
environment:
|
environment:
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
MONGO_HOST: "mongo"
|
||||||
MONGO_HOST: "mongo.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
STAGE: "production"
|
|
||||||
deploy:
|
|
||||||
mode: replicated
|
|
||||||
restart_policy:
|
|
||||||
condition: any
|
|
||||||
update_config:
|
|
||||||
parallelism: 1
|
|
||||||
order: start-first
|
|
||||||
|
|
||||||
updater:
|
|
||||||
image: mathwave/sprint-repo:pizda-bot
|
|
||||||
command: updater
|
|
||||||
environment:
|
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
|
||||||
MONGO_HOST: "mongo.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
|
networks:
|
||||||
|
- queues
|
||||||
|
- configurator
|
||||||
|
- mongo
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -41,13 +25,11 @@ services:
|
|||||||
image: mathwave/sprint-repo:pizda-bot
|
image: mathwave/sprint-repo:pizda-bot
|
||||||
command: api
|
command: api
|
||||||
environment:
|
environment:
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
MONGO_HOST: "mongo"
|
||||||
MONGO_HOST: "mongo.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
STAGE: "production"
|
|
||||||
networks:
|
networks:
|
||||||
- common-infra-nginx
|
- common-infra-nginx
|
||||||
|
- mongo
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -59,3 +41,9 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
common-infra-nginx:
|
common-infra-nginx:
|
||||||
external: true
|
external: true
|
||||||
|
queues:
|
||||||
|
external: true
|
||||||
|
configurator:
|
||||||
|
external: true
|
||||||
|
mongo:
|
||||||
|
external: true
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
name: Build and push
|
|
||||||
|
|
||||||
on: [push]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
name: Build
|
|
||||||
runs-on: [dev]
|
|
||||||
steps:
|
|
||||||
- name: login
|
|
||||||
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
- name: build
|
|
||||||
run: docker build -t mathwave/sprint-repo:pizda-bot .
|
|
||||||
push:
|
|
||||||
name: Push
|
|
||||||
runs-on: [dev]
|
|
||||||
needs: build
|
|
||||||
steps:
|
|
||||||
- name: push
|
|
||||||
run: docker push mathwave/sprint-repo:pizda-bot
|
|
||||||
@@ -1,26 +1,43 @@
|
|||||||
name: Deploy Dev
|
name: Deploy Dev
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- dev
|
- dev
|
||||||
types: [closed]
|
types: [closed]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
deploy-dev:
|
build:
|
||||||
name: Deploy dev
|
name: Build
|
||||||
runs-on: [dev]
|
runs-on: [ dev ]
|
||||||
needs: push
|
steps:
|
||||||
steps:
|
- name: login
|
||||||
- name: login
|
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||||
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
- name: checkout
|
||||||
- name: checkout
|
uses: actions/checkout@v4
|
||||||
uses: actions/checkout@v4
|
with:
|
||||||
with:
|
ref: dev
|
||||||
ref: dev
|
- name: build
|
||||||
- name: deploy
|
run: docker build -t mathwave/sprint-repo:pizda-bot .
|
||||||
env:
|
push:
|
||||||
TELEGRAM_TOKEN_DEV: ${{ secrets.TELEGRAM_TOKEN_DEV }}
|
name: Push
|
||||||
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
|
runs-on: [ dev ]
|
||||||
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
|
needs: build
|
||||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml pizda-bot
|
steps:
|
||||||
|
- name: push
|
||||||
|
run: docker push mathwave/sprint-repo:pizda-bot
|
||||||
|
deploy-dev:
|
||||||
|
name: Deploy dev
|
||||||
|
runs-on: [prod]
|
||||||
|
needs: push
|
||||||
|
steps:
|
||||||
|
- name: login
|
||||||
|
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: dev
|
||||||
|
- name: deploy
|
||||||
|
env:
|
||||||
|
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
|
||||||
|
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml pizda-bot-development
|
||||||
|
|||||||
@@ -1,21 +1,43 @@
|
|||||||
name: Deploy Prod
|
name: Deploy Prod
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- prod
|
- prod
|
||||||
types: [closed]
|
types: [closed]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
deploy-prod:
|
build:
|
||||||
name: Deploy prod
|
name: Build
|
||||||
runs-on: [dev]
|
runs-on: [ dev ]
|
||||||
steps:
|
steps:
|
||||||
- name: login
|
- name: login
|
||||||
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
ref: prod
|
ref: prod
|
||||||
- name: deploy
|
- name: build
|
||||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml pizda-bot
|
run: docker build -t mathwave/sprint-repo:pizda-bot .
|
||||||
|
push:
|
||||||
|
name: Push
|
||||||
|
runs-on: [ dev ]
|
||||||
|
needs: build
|
||||||
|
steps:
|
||||||
|
- name: push
|
||||||
|
run: docker push mathwave/sprint-repo:pizda-bot
|
||||||
|
deploy-prod:
|
||||||
|
name: Deploy prod
|
||||||
|
runs-on: [prod]
|
||||||
|
needs: push
|
||||||
|
steps:
|
||||||
|
- name: login
|
||||||
|
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||||
|
- name: checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: prod
|
||||||
|
- name: deploy
|
||||||
|
env:
|
||||||
|
MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }}
|
||||||
|
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml pizda-bot
|
||||||
|
|||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -117,3 +117,5 @@ GitHub.sublime-settings
|
|||||||
!.vscode/launch.json
|
!.vscode/launch.json
|
||||||
!.vscode/extensions.json
|
!.vscode/extensions.json
|
||||||
.history
|
.history
|
||||||
|
|
||||||
|
*pb2*
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
stages:
|
|
||||||
- build
|
|
||||||
- deploy-dev
|
|
||||||
- deploy-prod
|
|
||||||
|
|
||||||
build:
|
|
||||||
stage: build
|
|
||||||
tags:
|
|
||||||
- dev
|
|
||||||
before_script:
|
|
||||||
- docker login -u mathwave -p $DOCKERHUB_PASSWORD
|
|
||||||
script:
|
|
||||||
- docker build -t mathwave/sprint-repo:pizda-bot .
|
|
||||||
|
|
||||||
push:
|
|
||||||
stage: build
|
|
||||||
tags:
|
|
||||||
- dev
|
|
||||||
before_script:
|
|
||||||
- docker login -u mathwave -p $DOCKERHUB_PASSWORD
|
|
||||||
script:
|
|
||||||
- docker push mathwave/sprint-repo:pizda-bot
|
|
||||||
|
|
||||||
.deploy:
|
|
||||||
before_script:
|
|
||||||
- docker login -u mathwave -p $DOCKERHUB_PASSWORD
|
|
||||||
|
|
||||||
deploy-dev:
|
|
||||||
extends:
|
|
||||||
- .deploy
|
|
||||||
stage: deploy-dev
|
|
||||||
tags:
|
|
||||||
- dev
|
|
||||||
rules:
|
|
||||||
- if: '$CI_COMMIT_BRANCH == "master"'
|
|
||||||
when: on_success
|
|
||||||
- when: manual
|
|
||||||
script:
|
|
||||||
- docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml pizda-bot
|
|
||||||
|
|
||||||
deploy-prod:
|
|
||||||
extends:
|
|
||||||
- .deploy
|
|
||||||
stage: deploy-prod
|
|
||||||
tags:
|
|
||||||
- prod
|
|
||||||
only:
|
|
||||||
- master
|
|
||||||
when: manual
|
|
||||||
script:
|
|
||||||
- docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml pizda-bot
|
|
||||||
@@ -2,7 +2,7 @@ from collections import defaultdict
|
|||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
from mongo import mongo
|
from utils.mongo import mongo
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
13
daemons/base.py
Normal file
13
daemons/base.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
stage = os.getenv("STAGE", 'local')
|
||||||
|
if stage == 'local':
|
||||||
|
QUEUES_URL = 'localhost:50051'
|
||||||
|
else:
|
||||||
|
QUEUES_URL = 'queues-grpc:50051'
|
||||||
|
|
||||||
|
|
||||||
|
class Daemon:
|
||||||
|
def execute(self):
|
||||||
|
raise NotImplemented
|
||||||
@@ -2,7 +2,7 @@ import datetime
|
|||||||
import random
|
import random
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
from mongo import mongo
|
from utils.mongo import mongo
|
||||||
|
|
||||||
|
|
||||||
def update():
|
def update():
|
||||||
160
daemons/worker.py
Normal file
160
daemons/worker.py
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
import datetime
|
||||||
|
import os
|
||||||
|
from random import randrange, choice
|
||||||
|
from daemons import base
|
||||||
|
from utils import queues
|
||||||
|
from telebot.types import Message
|
||||||
|
import json
|
||||||
|
|
||||||
|
from utils.mongo import mongo
|
||||||
|
from utils.sprint_platform import PlatformClient
|
||||||
|
from utils.storage import set_values, get_chat_info
|
||||||
|
|
||||||
|
security_token = os.getenv("PLATFORM_SECURITY_TOKEN")
|
||||||
|
stage = os.getenv("STAGE", 'local')
|
||||||
|
|
||||||
|
|
||||||
|
client = PlatformClient(
|
||||||
|
security_token,
|
||||||
|
'Pizda Bot',
|
||||||
|
stage,
|
||||||
|
['constants', 'answers', 'replies'],
|
||||||
|
[],
|
||||||
|
need_poll=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
all_letters = "йцукенгшщзхъёфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪЁФЫВАПРОЛДЖЭЯЧСМИТЬБЮQWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890 "
|
||||||
|
|
||||||
|
|
||||||
|
def get_self_name():
|
||||||
|
return client.get_config('constants')['self_name']
|
||||||
|
|
||||||
|
|
||||||
|
def get_answers():
|
||||||
|
return client.get_config('answers')
|
||||||
|
|
||||||
|
|
||||||
|
def get_ignored_users():
|
||||||
|
return client.get_config('ignored_users')['users']
|
||||||
|
|
||||||
|
|
||||||
|
def get_replies():
|
||||||
|
return client.get_config('replies')
|
||||||
|
|
||||||
|
|
||||||
|
class Daemon(base.Daemon, queues.TasksHandlerMixin):
|
||||||
|
@property
|
||||||
|
def queue_name(self):
|
||||||
|
return 'pizda_bot_worker'
|
||||||
|
|
||||||
|
def execute(self):
|
||||||
|
self.poll()
|
||||||
|
|
||||||
|
def reply(self, text: str, chat_id: int, message_id: int):
|
||||||
|
queues.set_task(
|
||||||
|
'botalka_mailbox',
|
||||||
|
{
|
||||||
|
'project': 'pizda-bot',
|
||||||
|
'name': 'telegram-bot',
|
||||||
|
'body': {
|
||||||
|
'text': text,
|
||||||
|
'reply_to_message_id': message_id,
|
||||||
|
'chat_id': chat_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
def send(self, text: str, chat_id: int):
|
||||||
|
queues.set_task(
|
||||||
|
'botalka_mailbox',
|
||||||
|
{
|
||||||
|
'project': 'pizda-bot',
|
||||||
|
'name': 'telegram-bot',
|
||||||
|
'body': {
|
||||||
|
'text': text,
|
||||||
|
'chat_id': chat_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
def process_command(self, message: Message):
|
||||||
|
if message.text.startswith('/pointers'):
|
||||||
|
rating = list(mongo.counter_collection.find({"chat_id": message.chat.id, "points": {"$exists": True}}).sort("points", -1))
|
||||||
|
if not rating:
|
||||||
|
self.send('Ебаллы пока никому не начислялись', message.chat.id)
|
||||||
|
return
|
||||||
|
text = "Рейтинг Ебальников:\n"
|
||||||
|
rating_arr = list(enumerate(rating))
|
||||||
|
total = 0
|
||||||
|
for _, value in rating_arr:
|
||||||
|
total += value['points']
|
||||||
|
for index, value in rating_arr:
|
||||||
|
text += f"{index + 1}. @{value['username']} - {value['points']}\n"
|
||||||
|
self.send(text, message.chat.id)
|
||||||
|
elif message.text.startswith('/point'):
|
||||||
|
if not message.reply_to_message:
|
||||||
|
self.reply('Чтобы начислить Ебаллы, нужно прописать команду ответом на чье-то сообщение', message.chat.id, message.message_id)
|
||||||
|
return
|
||||||
|
username = message.reply_to_message.from_user.username
|
||||||
|
if username == get_self_name():
|
||||||
|
self.reply('Ты конченый? Как я себе Ебаллы то начислю, мудила? Мамке своей Ебаллы начисляй, пидор!', message.chat.id, message.message_id)
|
||||||
|
return
|
||||||
|
mongo.inc_points(username, message.chat.id)
|
||||||
|
self.reply('Ты конченый? Как я себе Ебаллы то начислю, мудила? Мамке своей Ебаллы начисляй, пидор!', message.chat.id, message.reply_to_message.message_id)
|
||||||
|
elif message.text.startswith('/rating'):
|
||||||
|
rating = list(mongo.counter_collection.find({"chat_id": message.chat.id}).sort("count", -1))
|
||||||
|
if not rating:
|
||||||
|
self.send('В этом чате я пока никому не парировал', message.chat.id)
|
||||||
|
return
|
||||||
|
text = "Вот кому я парировал:\n"
|
||||||
|
rating_arr = list(enumerate(rating))
|
||||||
|
total = 0
|
||||||
|
for _, value in rating_arr:
|
||||||
|
total += value['count']
|
||||||
|
for index, value in rating_arr:
|
||||||
|
text += f"{index + 1}. @{value['username']} - {value['count']} ({int(value['count'] / total * 100)}%)\n"
|
||||||
|
self.send(text, message.chat.id)
|
||||||
|
elif message.text.startswith('/setprobability'):
|
||||||
|
self.send('Отправь одно число - вероятность парирования', message.chat.id)
|
||||||
|
set_values(message.chat.id, state="set_probability")
|
||||||
|
|
||||||
|
def process(self, payload):
|
||||||
|
message: Message = Message.de_json(json.dumps(payload))
|
||||||
|
if not message.text:
|
||||||
|
return
|
||||||
|
if message.text.startswith('/'):
|
||||||
|
self.process_command(message)
|
||||||
|
return
|
||||||
|
if message.reply_to_message:
|
||||||
|
if message.reply_to_message.from_user.username == get_self_name():
|
||||||
|
self.reply(choice(get_replies()), message.chat.id, message.message_id)
|
||||||
|
return
|
||||||
|
info = get_chat_info(message.chat.id)
|
||||||
|
if client.get_config('updater')['enabled']:
|
||||||
|
set_values(message.chat.id, last_time_updated=datetime.datetime.now())
|
||||||
|
if message.text == '#debug' and client.is_staff(telegram_id=message.from_user.id):
|
||||||
|
self.reply(f'chat id: {message.chat.id}\nprobability: {info["probability"]}', message.chat.id, message.message_id)
|
||||||
|
return
|
||||||
|
if info['state'] == "set_probability":
|
||||||
|
try:
|
||||||
|
value = int(message.text)
|
||||||
|
if value < 0 or value > 100:
|
||||||
|
self.reply('Число не попадает в диапозон от 0 до 100!', message.chat.id, message.message_id)
|
||||||
|
else:
|
||||||
|
set_values(message.chat.id, probability=value, state="default")
|
||||||
|
self.reply('Ок! Установил', message.chat.id, message.message_id)
|
||||||
|
except ValueError:
|
||||||
|
self.reply('Это не число!', message.chat.id, message.message_id)
|
||||||
|
return
|
||||||
|
convert_text = ''.join([letter for letter in message.text if letter in all_letters]).lower().split()
|
||||||
|
if len(convert_text) > 0:
|
||||||
|
convert_text = convert_text[-1]
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
ans = get_answers().get(convert_text)
|
||||||
|
if ans is not None and randrange(1, 101) <= info["probability"] and message.from_user.id not in get_ignored_users():
|
||||||
|
self.reply(ans, message.chat.id, message.message_id)
|
||||||
|
mongo.inc(message.from_user.username, message.chat.id)
|
||||||
137
main.py
137
main.py
@@ -1,137 +1,12 @@
|
|||||||
import datetime
|
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
from random import randrange, choice
|
|
||||||
|
|
||||||
import telebot
|
|
||||||
from telebot.types import Message
|
|
||||||
|
|
||||||
from mongo import mongo
|
|
||||||
from sprint_platform import PlatformClient
|
|
||||||
from storage import set_values, get_chat_info
|
|
||||||
|
|
||||||
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
|
||||||
security_token = os.getenv("PLATFORM_SECURITY_TOKEN")
|
|
||||||
stage = os.getenv("STAGE", 'local')
|
|
||||||
|
|
||||||
|
|
||||||
client = PlatformClient(
|
|
||||||
security_token,
|
|
||||||
'Pizda Bot',
|
|
||||||
stage,
|
|
||||||
['constants', 'answers', 'replies'],
|
|
||||||
[],
|
|
||||||
need_poll=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
all_letters = "йцукенгшщзхъёфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪЁФЫВАПРОЛДЖЭЯЧСМИТЬБЮQWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890 "
|
|
||||||
|
|
||||||
|
|
||||||
def get_self_name():
|
|
||||||
return client.get_config('constants')['self_name']
|
|
||||||
|
|
||||||
|
|
||||||
def get_answers():
|
|
||||||
return client.get_config('answers')
|
|
||||||
|
|
||||||
|
|
||||||
def get_replies():
|
|
||||||
return client.get_config('replies')
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['setprobability'])
|
|
||||||
def set_probability(message: Message):
|
|
||||||
bot.send_message(message.chat.id, "Отправь одно число - вероятность парирования")
|
|
||||||
set_values(message.chat.id, state="set_probability")
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['rating'])
|
|
||||||
def show_rating(message: Message):
|
|
||||||
rating = list(mongo.counter_collection.find({"chat_id": message.chat.id}).sort("count", -1))
|
|
||||||
if not rating:
|
|
||||||
bot.send_message(message.chat.id, "В этом чате я пока никому не парировал")
|
|
||||||
return
|
|
||||||
text = "Вот кому я парировал:\n"
|
|
||||||
rating_arr = list(enumerate(rating))
|
|
||||||
total = 0
|
|
||||||
for _, value in rating_arr:
|
|
||||||
total += value['count']
|
|
||||||
for index, value in rating_arr:
|
|
||||||
text += f"{index + 1}. @{value['username']} - {value['count']} ({int(value['count'] / total * 100)}%)\n"
|
|
||||||
bot.send_message(message.chat.id, text)
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['point'])
|
|
||||||
def point(message: Message):
|
|
||||||
if not message.reply_to_message:
|
|
||||||
bot.reply_to(message, 'Чтобы начислить Ебаллы, нужно прописать команду ответом на чье-то сообщение')
|
|
||||||
return
|
|
||||||
username = message.reply_to_message.from_user.username
|
|
||||||
if username == get_self_name():
|
|
||||||
bot.reply_to(message, 'Ты конченый? Как я себе Ебаллы то начислю, мудила? Мамке своей Ебаллы начисляй, пидор!')
|
|
||||||
return
|
|
||||||
mongo.inc_points(username, message.chat.id)
|
|
||||||
bot.reply_to(message.reply_to_message, 'Тебе начислили Ебалл!')
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['pointers'])
|
|
||||||
def pointers(message: Message):
|
|
||||||
rating = list(mongo.counter_collection.find({"chat_id": message.chat.id, "points": {"$exists": True}}).sort("points", -1))
|
|
||||||
if not rating:
|
|
||||||
bot.send_message(message.chat.id, "Ебаллы пока никому не начислялись")
|
|
||||||
return
|
|
||||||
text = "Рейтинг Ебальников:\n"
|
|
||||||
rating_arr = list(enumerate(rating))
|
|
||||||
total = 0
|
|
||||||
for _, value in rating_arr:
|
|
||||||
total += value['points']
|
|
||||||
for index, value in rating_arr:
|
|
||||||
text += f"{index + 1}. @{value['username']} - {value['points']}\n"
|
|
||||||
bot.send_message(message.chat.id, text)
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler()
|
|
||||||
def do_action(message: Message):
|
|
||||||
if message.reply_to_message:
|
|
||||||
if message.reply_to_message.from_user.username == get_self_name():
|
|
||||||
bot.reply_to(message, choice(get_replies()))
|
|
||||||
return
|
|
||||||
info = get_chat_info(message.chat.id)
|
|
||||||
if client.get_config('updater')['enabled']:
|
|
||||||
set_values(message.chat.id, last_time_updated=datetime.datetime.now())
|
|
||||||
if message.text == '#debug' and client.is_staff(telegram_id=message.from_user.id):
|
|
||||||
bot.send_message(message.chat.id, f'chat id: {message.chat.id}\n'
|
|
||||||
f'probability: {info["probability"]}')
|
|
||||||
return
|
|
||||||
if info['state'] == "set_probability":
|
|
||||||
try:
|
|
||||||
value = int(message.text)
|
|
||||||
if value < 0 or value > 100:
|
|
||||||
bot.reply_to(message, "Число не попадает в диапозон от 0 до 100!")
|
|
||||||
else:
|
|
||||||
set_values(message.chat.id, probability=value, state="default")
|
|
||||||
bot.reply_to(message, "Ок! Установил")
|
|
||||||
except ValueError:
|
|
||||||
bot.reply_to(message, "Это не число!")
|
|
||||||
return
|
|
||||||
convert_text = ''.join([letter for letter in message.text if letter in all_letters]).lower().split()
|
|
||||||
if len(convert_text) > 0:
|
|
||||||
convert_text = convert_text[-1]
|
|
||||||
else:
|
|
||||||
return
|
|
||||||
ans = get_answers().get(convert_text)
|
|
||||||
if ans is not None and randrange(1, 101) <= info["probability"]:
|
|
||||||
bot.reply_to(message, ans)
|
|
||||||
mongo.inc(message.from_user.username, message.chat.id)
|
|
||||||
|
|
||||||
|
|
||||||
arg = sys.argv[-1]
|
arg = sys.argv[-1]
|
||||||
if arg == 'bot':
|
if arg == 'worker':
|
||||||
bot.polling()
|
from daemons import worker
|
||||||
elif arg == 'updater':
|
daemon = worker.Daemon()
|
||||||
from updater import update
|
|
||||||
update()
|
|
||||||
else:
|
else:
|
||||||
from api import app
|
from daemons.api import app
|
||||||
app.run(host="0.0.0.0", port=1238)
|
app.run(host="0.0.0.0", port=1238)
|
||||||
|
|
||||||
|
daemon.execute()
|
||||||
|
|||||||
10
settings.py
10
settings.py
@@ -1,10 +0,0 @@
|
|||||||
import os
|
|
||||||
import sys
|
|
||||||
import zoneinfo
|
|
||||||
|
|
||||||
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
|
||||||
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
|
||||||
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
|
|
||||||
|
|
||||||
CACHE_SIZE = int(os.getenv("CACHE_SIZE", 1000))
|
|
||||||
CACHE_TTL = int(os.getenv("CACHE_TTL", 3600))
|
|
||||||
0
utils/__init__.py
Normal file
0
utils/__init__.py
Normal file
@@ -1,13 +1,14 @@
|
|||||||
from functools import cached_property
|
|
||||||
|
|
||||||
import pymongo
|
import pymongo
|
||||||
|
import os
|
||||||
|
|
||||||
import settings
|
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
||||||
|
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
||||||
|
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
|
||||||
|
|
||||||
|
|
||||||
class Mongo:
|
class Mongo:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
url = f"mongodb://{settings.MONGO_USER}:{settings.MONGO_PASSWORD}@{settings.MONGO_HOST}:27017/"
|
url = f"mongodb://{MONGO_USER}:{MONGO_PASSWORD}@{MONGO_HOST}:27017/"
|
||||||
self.client = pymongo.MongoClient(url)
|
self.client = pymongo.MongoClient(url)
|
||||||
self.database = self.client.get_database("pizda-bot")
|
self.database = self.client.get_database("pizda-bot")
|
||||||
self.chats_collection.create_index([
|
self.chats_collection.create_index([
|
||||||
@@ -21,11 +22,11 @@ class Mongo:
|
|||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
return self.database.get_collection(item)
|
return self.database.get_collection(item)
|
||||||
|
|
||||||
@cached_property
|
@property
|
||||||
def chats_collection(self):
|
def chats_collection(self):
|
||||||
return self["chats"]
|
return self["chats"]
|
||||||
|
|
||||||
@cached_property
|
@property
|
||||||
def counter_collection(self):
|
def counter_collection(self):
|
||||||
return self["counter"]
|
return self["counter"]
|
||||||
|
|
||||||
85
utils/queues.py
Normal file
85
utils/queues.py
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
import datetime
|
||||||
|
import os
|
||||||
|
import zoneinfo
|
||||||
|
import requests
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
stage = os.getenv("STAGE", 'local')
|
||||||
|
if stage == 'local':
|
||||||
|
QUEUES_URL = 'http://localhost:1239'
|
||||||
|
else:
|
||||||
|
QUEUES_URL = 'http://queues:1239'
|
||||||
|
|
||||||
|
|
||||||
|
class QueuesException(Exception):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
class TasksHandlerMixin:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.executor = ThreadPoolExecutor(max_workers=1)
|
||||||
|
|
||||||
|
def _send_metric(self, start: datetime.datetime, end: datetime.datetime, success: bool):
|
||||||
|
def send():
|
||||||
|
requests.post(f'{QUEUES_URL}/api/v1/metric', json={
|
||||||
|
'service': 'botalka',
|
||||||
|
'queue': self.queue_name,
|
||||||
|
'success': success,
|
||||||
|
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
||||||
|
"success": success,
|
||||||
|
"execution_time_ms": (end - start).microseconds // 1000,
|
||||||
|
"environment": stage,
|
||||||
|
})
|
||||||
|
|
||||||
|
self.executor.submit(send)
|
||||||
|
|
||||||
|
def poll(self):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name}).json()
|
||||||
|
except requests.JSONDecodeError:
|
||||||
|
print('Unable to decode json')
|
||||||
|
time.sleep(3)
|
||||||
|
continue
|
||||||
|
task = response.get('task')
|
||||||
|
if not task:
|
||||||
|
time.sleep(0.2)
|
||||||
|
continue
|
||||||
|
start = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||||
|
try:
|
||||||
|
print(f'process task with id {task["id"]}, attempt {task["attempt"]}')
|
||||||
|
self.process(task['payload'])
|
||||||
|
success = True
|
||||||
|
except Exception as exc:
|
||||||
|
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
|
||||||
|
success = False
|
||||||
|
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||||
|
if success:
|
||||||
|
try:
|
||||||
|
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||||
|
if resp.status_code != 202:
|
||||||
|
raise QueuesException
|
||||||
|
print(f'finish task with id {task["id"]}')
|
||||||
|
except:
|
||||||
|
print(f'Failed to finish task id={task["id"]}')
|
||||||
|
self._send_metric(start, end, success)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def queue_name(self):
|
||||||
|
raise NotImplemented
|
||||||
|
|
||||||
|
def process(self, payload):
|
||||||
|
raise NotImplemented
|
||||||
|
|
||||||
|
|
||||||
|
def set_task(queue_name: str, payload: dict, seconds_to_execute: int, delay: int|None = None):
|
||||||
|
resp = requests.post(f'{QUEUES_URL}/api/v1/put', headers={'queue': queue_name}, json={
|
||||||
|
'payload': payload,
|
||||||
|
'seconds_to_execute': seconds_to_execute,
|
||||||
|
'delay': delay,
|
||||||
|
})
|
||||||
|
if resp.status_code != 202:
|
||||||
|
raise QueuesException
|
||||||
@@ -14,11 +14,8 @@ class PlatformClient:
|
|||||||
self.stage = stage
|
self.stage = stage
|
||||||
self.configs = configs
|
self.configs = configs
|
||||||
self.experiments = experiments
|
self.experiments = experiments
|
||||||
self.endpoint = 'https://platform.sprinthub.ru/'
|
self.endpoint = 'http://configurator/'
|
||||||
self.configs_url = urllib.parse.urljoin(self.endpoint, 'configs/get')
|
self.fetch_url = urllib.parse.urljoin(self.endpoint, '/api/v1/fetch')
|
||||||
self.experiments_url = urllib.parse.urljoin(self.endpoint, 'experiments/get')
|
|
||||||
self.staff_url = urllib.parse.urljoin(self.endpoint, 'is_staff')
|
|
||||||
self.fetch_url = urllib.parse.urljoin(self.endpoint, 'fetch')
|
|
||||||
self.config_storage = {}
|
self.config_storage = {}
|
||||||
self.experiment_storage = {}
|
self.experiment_storage = {}
|
||||||
self.staff_storage = {}
|
self.staff_storage = {}
|
||||||
@@ -43,7 +40,6 @@ class PlatformClient:
|
|||||||
try:
|
try:
|
||||||
response = get(
|
response = get(
|
||||||
url,
|
url,
|
||||||
headers={'X-Security-Token': self.platform_security_token},
|
|
||||||
params=params
|
params=params
|
||||||
)
|
)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
from cachetools import TTLCache
|
from cachetools import TTLCache
|
||||||
|
import os
|
||||||
|
|
||||||
import settings
|
from utils.mongo import mongo
|
||||||
from mongo import mongo
|
|
||||||
|
|
||||||
cache = TTLCache(settings.CACHE_SIZE, settings.CACHE_TTL)
|
CACHE_SIZE = int(os.getenv("CACHE_SIZE", 1000))
|
||||||
|
CACHE_TTL = int(os.getenv("CACHE_TTL", 3600))
|
||||||
|
|
||||||
|
cache = TTLCache(CACHE_SIZE, CACHE_TTL)
|
||||||
|
|
||||||
|
|
||||||
def get_chat_info(chat_id: int) -> dict:
|
def get_chat_info(chat_id: int) -> dict:
|
||||||
Reference in New Issue
Block a user