Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>'
4 changes: 2 additions & 2 deletions .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
- name: Set up Python 3.9
uses: actions/setup-python@v1
with:
python-version: 3.8
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
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.
42 changes: 19 additions & 23 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
from enum import Enum
from telebot.types import ReplyKeyboardMarkup
from telebot import TeleBot
from dotenv import dotenv_values
import logging
from sys import stdout

from src.sql_utils import SQLighter

Expand All @@ -14,7 +14,16 @@
SUPPORT_ACCOUNT = dotenv_values('.env')['SUPPORT_ACCOUNT']

# Set logging format
logging.basicConfig(filename='cyberdbot.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
LOG_FILE = 'cyberdbot.log'
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(module)s - %(message)s',
datefmt='%d-%m-%Y %H:%M:%S',
handlers=[
logging.FileHandler(LOG_FILE),
logging.StreamHandler(stdout)
]
)

# cyberdBot key name in the cyber
CYBER_KEY_NAME = dotenv_values('.env')['CYBER_KEY_NAME']
Expand All @@ -32,25 +41,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 All @@ -74,23 +70,23 @@
TOKEN_NAME = 'BOOT'

# List of commands
COMMAND_LIST = ['search', 'cyberlink', 'ipfs', 'tweet', 'check', 'validators', 'issue']
COMMAND_LIST = ['search', 'cyberlink', 'ipfs', 'check', 'validators', 'issue']

# Base Menu
BASE_MENU = ['Search', 'Create cyberLink', 'Upload to IPFS', 'Sign up',
BASE_MENU = ['Search a content in Bostrom', 'Create cyberLink', 'Upload to IPFS',
'Jail check', 'Validator list', 'Jail check settings']
BASE_KEYBOARD = ReplyKeyboardMarkup(True, True)
BASE_KEYBOARD.add(BASE_MENU[0])
BASE_KEYBOARD.add(BASE_MENU[1], BASE_MENU[2], BASE_MENU[3])
BASE_KEYBOARD.add(BASE_MENU[4], BASE_MENU[5], BASE_MENU[6])
BASE_KEYBOARD.add(BASE_MENU[2])
BASE_KEYBOARD.add(BASE_MENU[3], BASE_MENU[4], BASE_MENU[5])

# BASE Menu after Sign up
BASE_AFTER_SIGN_UP_MENU = ['Search', 'Create cyberLink', 'Upload to IPFS', 'Tweet',
BASE_AFTER_SIGN_UP_MENU = ['Search a content in Bostrom', 'Create cyberLink', 'Upload to IPFS',
'Jail check', 'Validator list', 'Jail check settings']
BASE_AFTER_SIGN_UP_KEYBOARD = ReplyKeyboardMarkup(True, True)
BASE_AFTER_SIGN_UP_KEYBOARD.add(BASE_AFTER_SIGN_UP_MENU[0])
BASE_AFTER_SIGN_UP_KEYBOARD.add(BASE_AFTER_SIGN_UP_MENU[1], BASE_AFTER_SIGN_UP_MENU[2], BASE_AFTER_SIGN_UP_MENU[3])
BASE_AFTER_SIGN_UP_KEYBOARD.add(BASE_AFTER_SIGN_UP_MENU[4], BASE_AFTER_SIGN_UP_MENU[5], BASE_AFTER_SIGN_UP_MENU[6])
BASE_AFTER_SIGN_UP_KEYBOARD.add(BASE_AFTER_SIGN_UP_MENU[2])
BASE_AFTER_SIGN_UP_KEYBOARD.add(BASE_AFTER_SIGN_UP_MENU[3], BASE_AFTER_SIGN_UP_MENU[4], BASE_AFTER_SIGN_UP_MENU[5])

BASE_MENU_LOWER = set(map(str.lower, BASE_MENU + BASE_AFTER_SIGN_UP_MENU + ['/' + item for item in COMMAND_LIST]))

Expand Down Expand Up @@ -121,7 +117,7 @@ class States(Enum):
S_SEARCH = 1 # Search a content
S_UPLOAD_IPFS = 2 # Upload a content to IPFS
S_STARTPOINT_CYBERLINK = 3 # Set a starting point of cyberlink
S_ENDPOINT_CYBERLINK = 4 # Set a endpoint of cyberlink
S_ENDPOINT_CYBERLINK = 4 # Set an endpoint of cyberlink
S_MONITORING = 5 # Jail checker
S_SIGNUP = 6 # Sign up a new cyberd account
S_NEW_TWEET = 7 # Upload a New Tweet
41 changes: 26 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from collections import defaultdict
import time
import re
from argparse import ArgumentParser

from cyberutils.bash import display_sleep

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 @@ -86,9 +89,9 @@ def search(message, number_of_search_results: int = 5):
search_list = search_list[:min(len(search_list), number_of_search_results)]
message_text = \
f'Top {number_of_search_results} search results:\n' + ''.join(
f'<u><a href="https://ipfs.io/ipfs/{item["particle"]}">{item["particle"]}</a></u>\n'
f'<u><a href="{CYBERPAGE_BASE_URL}/oracle/ask/{item["particle"]}">{item["particle"]}</a></u>\n'
for item in search_list) + \
f'\nother results on the <u><a href="{CYBERPAGE_BASE_URL}/search/{ipfs_hash}">cyb.ai</a></u>'
f'\nother results on the <u><a href="{CYBERPAGE_BASE_URL}/oracle/ask/{ipfs_hash}">cyb.ai</a></u>'
elif search_error == 'CID not found':
message_text = 'The content identifier not found in the cyber graph'
else:
Expand Down Expand Up @@ -167,9 +170,9 @@ def endpoint_cyberlink(message):
reply_markup=base_keyboard_reply_markup(message.from_user.id))
bot.send_message(
message.chat.id,
f'from: <u><a href="https://ipfs.io/ipfs/{cyberlink_startpoint_ipfs_hash[message.chat.id]}">'
f'from: <u><a href="{CYBERPAGE_BASE_URL}/oracle/ask/{cyberlink_startpoint_ipfs_hash[message.chat.id]}">'
f'{cyberlink_startpoint_ipfs_hash[message.chat.id]}</a></u>\n'
f'to: <u><a href="https://ipfs.io/ipfs/{ipfs_hash}">{ipfs_hash}</a></u>',
f'to: <u><a href="{CYBERPAGE_BASE_URL}/oracle/ask/{ipfs_hash}">{ipfs_hash}</a></u>',
parse_mode='HTML',
reply_markup=base_keyboard_reply_markup(message.from_user.id))
db_worker.write_cyberlink(
Expand Down Expand Up @@ -306,14 +309,14 @@ def main_menu(message):
parse_mode="HTML",
reply_markup=base_keyboard_reply_markup(message.from_user.id))

elif message.text.lower() == 'jail check settings':
elif message.text.lower() in ['jail check settings', '/settings']:
state[message.chat.id] = States.S_MONITORING
bot.send_message(
message.chat.id,
'Enter a validator moniker',
reply_markup=MONITORING_KEYBOARD)

elif message.text.lower() in ['search', '/search']:
elif message.text.lower() in ['search a content in bostrom', '/search']:
state[message.chat.id] = States.S_SEARCH
bot.send_message(
message.chat.id,
Expand Down Expand Up @@ -543,8 +546,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 All @@ -555,7 +562,11 @@ def tweet_menu(message):
bot.polling(
none_stop=True,
timeout=100)
display_sleep(30)
except KeyboardInterrupt:
logging.info('Stopped by Owner')
break
except Exception as e:
logging.error(f'Error in the main: {e}. Restart in 15 sec')
# restart in 15 sec
time.sleep(15)
logging.error(f'Error: {e}. Restart in 30 sec')
display_sleep(30)

24 changes: 17 additions & 7 deletions monitoring_scheduler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import time
from argparse import ArgumentParser

from cyberutils.bash import display_sleep

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 @@ -14,19 +16,27 @@ def check_send_messages():
for chat_id in chat_id_list:
jail_check(chat_id, pressed_button=False)
logging.info(f'Validators status sent for {len(chat_id_list)} users')
time.sleep(SCHEDULER_TIME)
display_sleep(SCHEDULER_TIME)


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
while True:
try:
check_send_messages()
display_sleep(30)
except KeyboardInterrupt:
logging.info(f'Stopped by Owner')
break
except Exception as e:
logging.error(f'Error {e}. It will be restarting in 15 sec')
time.sleep(15)
logging.error(f'Error: {e}. Restart in 30 sec')
display_sleep(30)
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
Loading