Skip to content

Commit

Permalink
- add makefile
Browse files Browse the repository at this point in the history
- update config and requirements
- remove unused functions in bash utils
- remove unused files
  • Loading branch information
Snedashkovsky committed Oct 22, 2023
1 parent e865470 commit 7008a89
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 186 deletions.
3 changes: 0 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ SUPPORT_ACCOUNT='<your_telegram_account>'
CYBERLINK_CREATION_QUERY='expect src/create_cyberlink.exp'
ACCOUNT_CREATION_QUERY='expect src/create_account.exp'
TRANSFER_QUERY='expect src/transfer_tokens.exp'
DELEGATE_QUERY='expect src/delegate_tokens.exp'
INVESTMINT_QUERY='expect src/investmint_tokens.exp'
UNJAIL_VALIDATOR_QUERY='expect src/unjail_validator.exp'
IPFS_RESTART_QUERY='expect src/restart_ipfs_node.exp'
IPFS_HOST='http://localhost:5001'
VALIDATOR_ADDRESS='<validator_address>'
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
default: help

all : help install_venv clean_venv test start_main start_dev_mode_main
.PHONY : all

help: # show help for each of the makefile recipes
@grep -E '^[a-zA-Z0-9 -_]+:.*#' Makefile | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done

install_venv: # install python virtual environment and requirements in it
test -d venv || python3 -m venv venv
. venv/bin/activate; pip install -Ur requirements.txt

clean_venv: # clean python virtual environment and requirements in it
rm -rf venv

test: # test cyberdBot
. venv/bin/activate; python3 -m pytest -s -v *.py src/*.py

start_main: # start main bot
. venv/bin/activate; python3 main.py

start_dev_mode_main: export VALIDATOR_QUERY=cat\ .\/tests\/validators_query_test
start_dev_mode_main: # start main bot in development mode for easy bot stop
. venv/bin/activate; python3 main.py --dev_mode

start_scheduler: # start scheduler
. venv/bin/activate; python3 monitoring_scheduler.py

start_dev_mode_scheduler: export VALIDATOR_QUERY=cat\ .\/tests\/validators_query_test
start_dev_mode_scheduler: # start scheduler in development mode for easy bot stop
. venv/bin/activate; python3 monitoring_scheduler.py --dev_mode
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
# cyberdBot [t.me/cyberdbot](https://t.me/cyberdbot)
Telegram bot for creation cyberLinks on the cyberd knoledge graph, upload content to IPFS and monitoring [cyberd](https://github.com/cybercongress/cyberd/) node status data
## Install
Install [IPFS node](https://docs-beta.ipfs.io/install/command-line-quick-start/)
Install [cyberd node](https://cybercongress.ai/docs/cyberd/run_validator/)
Clone repository:
- Install [IPFS node](https://docs-beta.ipfs.io/install/command-line-quick-start/)
- Install [cyberd node](https://cybercongress.ai/docs/cyberd/run_validator/)
- Clone repository:
```bash
git clone https://github.com/Snedashkovsky/cyberdBot
```
Install requirements
- Install requirements
```bash
pip3 install --user -r cyberdBot/requirements.txt
make install_venv
sudo apt-get install expect
```
Add your Telegram Bot Token, cyberd key name and cyberd passphrase into `start_bot.sh`
- Add your Telegram Bot Token, cyberd key name and cyberd passphrase into `.env`
## Run
### Main Bot
```bash
./start_bot.sh m|main|s|scheduler [d|dev]

Using:
m|main - Main Bot
s|scheduler - Monitoring Scheduler
[d|dev] - Development Mode
# Development Mode
make start_dev_mode_main
# Production mode
make start_main
```
### Monitoring Scheduler
```bash
# Development Mode
make start_dev_mode_scheduler
# Production mode
make start_scheduler
```
## Test
```
python3 -m pytest -s -v *.py src/*.py
make test
```

## Commands
Expand All @@ -40,7 +46,7 @@ issue - Create issue or send feedback
```

## Requirements
Python 3.6 or higher
Python 3.9 or higher

## Data for the Bostrom Genesis
[Bot user addresses with the number of created cyberlinks](https://ipfs.io/ipfs/QmWLoxH5F1tFvoiMEq8JEGjHsrT7JSkRxzhUGV1Lrn1GWk) as of 10/08/2021.
14 changes: 0 additions & 14 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from enum import Enum
from telebot.types import ReplyKeyboardMarkup
from telebot import TeleBot
Expand Down Expand Up @@ -32,25 +31,12 @@
# Shell query for transfer main tokens to new account
TRANSFER_QUERY = dotenv_values('.env')['TRANSFER_QUERY']

# Shell query for delegate main tokens to validator
DELEGATE_QUERY = dotenv_values('.env')['DELEGATE_QUERY']
VALIDATOR_ADDRESS = dotenv_values('.env')['VALIDATOR_ADDRESS']

# Shell query for investmint stake tokens for amper or volt
INVESTMINT_QUERY = dotenv_values('.env')['INVESTMINT_QUERY']

# Shell query for unjail validator
UNJAIL_VALIDATOR_QUERY = dotenv_values('.env')['UNJAIL_VALIDATOR_QUERY']

# Shell query for IPFS node restart
IPFS_RESTART_QUERY = dotenv_values('.env')['IPFS_RESTART_QUERY']

# IPFS HOST
IPFS_HOST = dotenv_values('.env')['IPFS_HOST']

# Development mode for easy bot stop (set in start_bot.sh)
DEV_MODE = int(os.getenv('DEV_MODE', 0))

# SQLite file name and DB worker
DB_FILE = 'db_sqlite.vdb'
db_worker = SQLighter(DB_FILE)
Expand Down
15 changes: 10 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from collections import defaultdict
import time
import re
from argparse import ArgumentParser

from src.bot_utils import create_temp_directory, send_ipfs_notification, jail_check, dict_to_md_list, \
message_upload_to_ipfs, base_keyboard_reply_markup
from src.lcd_utils import validators_state, search_cid
from src.bash_utils import create_cyberlink, create_account, transfer_tokens
from src.bash_utils import create_cyberlink, create_account
from config import CYBER_KEY_NAME, BASE_MENU_LOWER, MONITORING_MENU_LOWER, TWEETER_MENU_LOWER, MONITORING_KEYBOARD, \
TWEETER_KEYBOARD, TWEET_HASH, AVATAR_HASH, FOLLOW_HASH, DEV_MODE, States, bot, db_worker, CYBERPAGE_URL, \
CYBERPAGE_BASE_URL, TOKEN_NAME, COMMAND_LIST, SUPPORT_ACCOUNT, logging
TWEETER_KEYBOARD, TWEET_HASH, AVATAR_HASH, FOLLOW_HASH, States, bot, db_worker, CYBERPAGE_URL, \
CYBERPAGE_BASE_URL, COMMAND_LIST, SUPPORT_ACCOUNT, logging

# Create directory for temporary files
create_temp_directory()
Expand Down Expand Up @@ -543,8 +544,12 @@ def tweet_menu(message):

if __name__ == '__main__':

if DEV_MODE:
print('DEV_MODE')
parser = ArgumentParser()
parser.add_argument("--dev_mode", action='store_true')
args = parser.parse_args()

if args.dev_mode:
print('DEV MODE')
bot.polling(
none_stop=True,
timeout=100)
Expand Down
11 changes: 8 additions & 3 deletions monitoring_scheduler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import time
from argparse import ArgumentParser

from src.bot_utils import jail_check
from config import SCHEDULER_TIME, DEV_MODE, db_worker, logging
from config import SCHEDULER_TIME, db_worker, logging


# Create tables
Expand All @@ -19,8 +20,12 @@ def check_send_messages():

if __name__ == '__main__':

if DEV_MODE:
print('DEV_MODE')
parser = ArgumentParser()
parser.add_argument("--dev_mode", action='store_true')
args = parser.parse_args()

if args.dev_mode:
print('DEV MODE')
check_send_messages()
else:
# Handler to avoid disconnection
Expand Down
18 changes: 10 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pytelegrambotapi
cyberpy~=1.0.5
pandas~=1.1.4
pysqlite3
requests~=2.25.1
pytelegrambotapi~=4.14.0
cyberpy~=1.0.9
cyberutils~=0.0.5
pandas~=1.3.5
pysqlite3~=0.4.8
requests~=2.28.2
gql[all]~=3.0.0a5
python-dotenv~=0.17.1
cachetools~=4.2.1
numpy~=1.19.4
python-dotenv~=0.21.1
cachetools~=4.2.4
numpy~=1.22.4
pytest~=7.4.2
80 changes: 3 additions & 77 deletions src/bash_utils.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
from subprocess import Popen, PIPE
import re

from cyberutils.bash import execute_bash

from config import CYBERLINK_CREATION_QUERY, ACCOUNT_CREATION_QUERY, TRANSFER_QUERY, IPFS_RESTART_QUERY, \
UNJAIL_VALIDATOR_QUERY, DELEGATE_QUERY, INVESTMINT_QUERY, VALIDATOR_ADDRESS, TOKEN_NAME, logging


def execute_bash(bash_command: str):
if len(bash_command.split('"')) == 1:
_bash_command_list = bash_command.split()
elif len(bash_command.split('"')) == 2:
_bash_command_list = \
bash_command.split('"')[0].split() + \
[bash_command.split('"')[1]]
elif len(bash_command.split('"')) > 2:
_bash_command_list = \
bash_command.split('"')[0].split() + \
[bash_command.split('"')[1]] + \
[item for items in bash_command.split('"')[2:] for item in items.split()]
else:
return None, f'Cannot split bash command {bash_command}'
popen_process = Popen(_bash_command_list, stdout=PIPE)
return popen_process.communicate(timeout=15)
TOKEN_NAME, logging


def extract_from_console(console_output: bytes, keys: list) -> list:
Expand Down Expand Up @@ -123,63 +106,6 @@ def transfer_tokens(account_address: str, value: int, token_name: str = TOKEN_NA
return None, error_transfer_tokens


def delegate_tokens(account_address: str, value: int, validator: str = VALIDATOR_ADDRESS, query: str = DELEGATE_QUERY):
try:
_output, error_execute_bash = \
execute_bash(f'{query} {account_address} {str(value) + TOKEN_NAME.lower()} {validator}')
if len(extract_from_console(_output, ['txhash'])[0][1]) > 0 \
or len(extract_from_console(_output, ['txhash'])[0][1]) > 0:
logging.info(
f"Tokens was delegated from {account_address} value {value}{TOKEN_NAME} "
f"txhash {extract_from_console(_output, ['txhash'])[0][1]}")
return True, None
logging.error(
f"Tokens was not delegated from {account_address} value {value}{TOKEN_NAME}. Error {error_execute_bash}")
return None, error_execute_bash
except Exception as error_transfer_tokens:
logging.error(
f"Tokens was not delegated from {account_address} value {value}{TOKEN_NAME}. Error {error_transfer_tokens}")
return None, error_transfer_tokens


def investmint_tokens(account_address: str, value: int, investmint_token: str = 'amper', query: str = INVESTMINT_QUERY):
try:
_output, error_execute_bash = \
execute_bash(f'{query} {account_address} {str(value) + "s" + TOKEN_NAME.lower()} {investmint_token}')
if len(extract_from_console(_output, ['txhash'])[0][1]) > 0:
logging.info(
f"Tokens was investminted from {account_address} to {investmint_token} value {value}s{TOKEN_NAME} "
f"txhash {extract_from_console(_output, ['txhash'])[0][1]}")
return True, None
logging.error(
f"Tokens was not investminted from {account_address} to {investmint_token} value {value}s{TOKEN_NAME}. "
f"Error {error_execute_bash}")
return None, error_execute_bash
except Exception as error_transfer_tokens:
logging.error(
f"Tokens was not investminted from {account_address} to {investmint_token} value {value}s{TOKEN_NAME}. "
f"Error {error_transfer_tokens}")
return None, error_transfer_tokens


def unjail_validator(query: str = UNJAIL_VALIDATOR_QUERY):
try:
_output, error_execute_bash = \
execute_bash(f'{query}')
if len(extract_from_console(_output, ['txhash'])[0][1]) > 0:
logging.info(
f"Unjail transaction was completed successfully"
f"txhash {extract_from_console(_output, ['txhash'])[0][1]}")
return True, None
logging.error(
f"Unjail transaction was not completed successfully. Error {error_execute_bash}")
return None, error_execute_bash
except Exception as error_transfer_tokens:
logging.error(
f"Unjail transaction was not completed successfully. Error {error_transfer_tokens}")
return None, error_transfer_tokens


def restart_ipfs_node(query: str = IPFS_RESTART_QUERY):
try:
output, error_execute_bash = \
Expand Down
10 changes: 0 additions & 10 deletions src/delegate_tokens.exp

This file was deleted.

11 changes: 0 additions & 11 deletions src/investmint_tokens.exp

This file was deleted.

8 changes: 0 additions & 8 deletions src/unjail_validator.exp

This file was deleted.

33 changes: 0 additions & 33 deletions start_bot.sh

This file was deleted.

0 comments on commit 7008a89

Please sign in to comment.