Skip to content
Permalink
Browse files

Merge branch 'master' of https://github.com/umputun/rt-bot into fix_m…

…emberries_config
  • Loading branch information...
selevit committed Nov 20, 2016
2 parents 8898107 + e57e5e1 commit 58bf443a93d6ea8daf8f202204dee2edb4b49837
Showing 694 changed files with 363,158 additions and 17 deletions.
3 .env
@@ -0,0 +1,3 @@
LETSENCRYPT=true
SSL_KEY=le-key.pem
SSL_CERT=le-crt.pem
@@ -0,0 +1,5 @@
node_modules/
.idea
*.pyc
__pycache__
etc/ssl/*
@@ -0,0 +1,50 @@
language: python

python:
- "3.5"

services:
- docker

cache:
directories:
- $HOME/.cache/pip

env:
global:
- SSL_KEY=nginx.key
- SSL_CERT=nginx.crt
- LETSENCRYPT=false
- DOCKER_REGISTRY_HOST=docker.io
- DOCKER_REGISTRY_LOGIN=
- DOCKER_REGISTRY_PASSWORD=
- BOTS_WAIT_TIMEOUT=10

before_install:
- pip install docker-compose==1.9.0
- openssl req -x509 -nodes -days 365 -newkey rsa:2048 -subj "/C=US/ST=Test/L=Test/O=Dis/CN=localhost" -keyout "etc/ssl/${SSL_KEY}" -out "etc/ssl/${SSL_CERT}"

install:
- pip install -r ci/requirements.txt
- docker-compose build

before_script:
- docker-compose up -d

script:
- set -e
- echo "Waiting nginx-bot to launch on nginx-bot:443..."
- while ! wget --no-check-certificate https://localhost/ping &> /dev/null; do sleep 1; done
- echo "Waiting $BOTS_WAIT_TIMEOUT seconds to ensure if all bots are ready..."; sleep "$BOTS_WAIT_TIMEOUT"
- BOT_BASE_URL="https://localhost" python ci/run_bot_tests.py .
- |
if [ "$TRAVIS_BRANCH" = "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
docker login \
--username "$DOCKER_REGISTRY_LOGIN" \
--password "$DOCKER_REGISTRY_PASSWORD" \
"$DOCKER_REGISTRY_HOST"
docker-compose push
fi
after_script:
- docker-compose down
@@ -0,0 +1,8 @@
FROM php:7.0-alpine

COPY ./src /bot
WORKDIR /bot/

EXPOSE 8080

CMD ["php", "-S", "0.0.0.0:8080"]
@@ -0,0 +1,6 @@
#Пятиминутка PHP - бот для Радио-Т

Если в чате упоминается PHP, то бот сообщает какой-то интересный и полезный факт об этом языке!
База знаний постоянно пополняется!

Оригинальный репозиторий бота: https://github.com/pqr/5minphp-bot
@@ -0,0 +1,14 @@
bot_name: "5minphp-bot"
bot_display_name: "Пятиминутка PHP"

ignored_commands:
- "Привет"
- "Как дела?"
- "Один два три"

test_cases:
- command: "пятиминутка, обновись"
result: "^У меня для вас есть свежие факты про PHP."

- command: "расскажи о php"
result: "^.*(PHP)?(php)?.*$"
@@ -0,0 +1,7 @@
5minphp-bot:
build: .
container_name: 5minphp-bot
hostname: 5minphp-bot
restart: always
ports:
- "8080:8080"
@@ -0,0 +1,92 @@
<?php
require_once __DIR__ . '/rollbar.php';
// set-up error handling via rollbar
Rollbar::init([
// required
'access_token' => '916b71816d754c3abd77b300d4d9941c',
// sets whether errors suppressed with '@' should be reported or not
'report_suppressed' => true,
// optional - path to directory your code is in. used for linking stack traces.
'root' => __DIR__
]);
define('LOCAL_FILE', __DIR__ . '/phpfact.txt');
define('REMOTE_FILE', 'https://raw.githubusercontent.com/pqr/5minphp-bot/master/phpfact.txt');
define('BOT_NAME', 'Пятиминутка PHP');
if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === '/info') {
sendResponseJson(200, [
'author' => '[@5minphp](https://twitter.com/5minphp)',
'info' => 'бот "Пятиминутка PHP" расскажет интересный и полезный факт об этом языке программирования, при упоминании PHP',
'commands' => []
]);
exit();
}
$input = file_get_contents('php://input');
if (!$input) {
http_response_code(417);
exit();
}
$inputData = @json_decode($input, true);
if (!$inputData || !isset($inputData['text']) || !is_string($inputData['text'])) {
http_response_code(417);
exit();
}
if (mb_stripos($inputData['text'], 'пятиминутка, обновись') !== false) {
downloadFacts();
sendResponseJson(201, [
'text' => 'У меня для вас есть свежие факты про PHP. Просто напишите сообщение с текстом содержащим "PHP", и вы узнаете, что...',
'bot' => BOT_NAME
]);
exit();
}
if (!preg_match('/\b(php\d*|пхп\d*)\b/iu', $inputData['text'])) {
http_response_code(417);
exit();
}
$fact = getRandomFact($inputData['text']);
if (!$fact) {
http_response_code(417);
exit();
}
sendResponseJson(201, ['text' => $fact, 'bot' => BOT_NAME]);
function getRandomFact(string $inputData): string
{
if (!@file_exists(LOCAL_FILE) || time() - @filemtime(LOCAL_FILE) > 3600) {
downloadFacts();
}
$allFacts = @file(LOCAL_FILE, FILE_IGNORE_NEW_LINES);
if (!$allFacts) {
return '';
}
$allFacts = array_filter($allFacts);
if (!$allFacts) {
return '';
}
return $allFacts[random_int(0, count($allFacts) - 1)];
}
function downloadFacts()
{
$newFacts = @file_get_contents(REMOTE_FILE);
if ($newFacts) {
@file_put_contents(LOCAL_FILE, $newFacts);
}
}
function sendResponseJson($code, $data)
{
http_response_code($code);
header('Content-Type: application/json;charset=UTF-8');
print json_encode($data);
}
Oops, something went wrong.

0 comments on commit 58bf443

Please sign in to comment.
You can’t perform that action at this time.