Skip to content

Commit

Permalink
Merge pull request #307 from sysblok/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyqu authored Mar 9, 2024
2 parents 50a0437 + bd84668 commit be7889a
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 59 deletions.
38 changes: 19 additions & 19 deletions .github/workflows/publish_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,22 @@ jobs:
}' \
https://api.telegram.org/bot${{env.TELEGRAM_TEST_TOKEN}}/sendMessage
integration_tests:
needs: push

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
ref: dev

- name: Integration tests
uses: fylein/python-pytest-github-action@v2
env:
CONFIG_OVERRIDE: ${{ secrets.CONFIG_OVERRIDE_TESTING }}
with:
args: |
apt-get update && apt-get install -y git && \
pip3 install -e . && pip3 install -r requirements.txt && \
pytest tests/integration -vs
# integration_tests:
# needs: push

# runs-on: ubuntu-latest

# steps:
# - uses: actions/checkout@v2
# with:
# ref: dev

# - name: Integration tests
# uses: fylein/python-pytest-github-action@v2
# env:
# CONFIG_OVERRIDE: ${{ secrets.CONFIG_OVERRIDE_TESTING }}
# with:
# args: |
# apt-get update && apt-get install -y git && \
# pip3 install -e . && pip3 install -r requirements.txt && \
# pytest tests/integration -vs
38 changes: 19 additions & 19 deletions .github/workflows/publish_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,22 @@ jobs:
-d '{"parse_mode": "markdown", "chat_id": ${{ env.TELEGRAM_ERROR_CHAT_ID }}, "text": "[github CI] deploy [failed](https://github.com/sysblok/sysblokbot/actions/runs/${{github.run_id}})"}' \
https://api.telegram.org/bot${{env.TELEGRAM_TOKEN}}/sendMessage
integration_tests:
needs: push

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
ref: master

- name: Integration tests
uses: fylein/python-pytest-github-action@v2
env:
CONFIG_OVERRIDE: ${{ secrets.CONFIG_OVERRIDE_PROD }}
with:
args: |
apt-get update && apt-get install -y git && \
pip3 install -e . && pip3 install -r requirements.txt && \
pytest tests/integration -vs
# integration_tests:
# needs: push

# runs-on: ubuntu-latest

# steps:
# - uses: actions/checkout@v2
# with:
# ref: master

# - name: Integration tests
# uses: fylein/python-pytest-github-action@v2
# env:
# CONFIG_OVERRIDE: ${{ secrets.CONFIG_OVERRIDE_PROD }}
# with:
# args: |
# apt-get update && apt-get install -y git && \
# pip3 install -e . && pip3 install -r requirements.txt && \
# pytest tests/integration -vs
6 changes: 6 additions & 0 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ def init_handlers(self):
direct_message_only(handlers.get_tasks_report),
"получить список задач из Trello",
)
self.add_manager_handler(
"get_tasks_report_advanced",
CommandCategories.SUMMARY,
direct_message_only(handlers.get_tasks_report_advanced),
"получить список задач из Trello (расширенный)",
)
self.add_manager_handler(
"get_articles_arts",
CommandCategories.SUMMARY,
Expand Down
16 changes: 13 additions & 3 deletions src/facebook/facebook_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

import dateutil.parser as dateparser
import facebook
import requests

from ..consts import ReportPeriod
from ..utils.singleton import Singleton
from .facebook_objects import FacebookPage

logger = logging.getLogger(__name__)
BASE_URL = 'https://graph.facebook.com'
API_VERSION = 'v10.0'


class FacebookClient(Singleton):
Expand All @@ -32,13 +35,20 @@ def _update_from_config(self):
self._api_client = facebook.GraphAPI(self._facebook_config["token"], 7.0)
self._page_id = self._facebook_config["page_id"]

def _make_graph_api_call(self, uri: str, params: dict) -> dict:
params['access_token'] = self._facebook_config["token"]
response = requests.get(
'/'.join([BASE_URL, API_VERSION, uri]) + '?' +
'&'.join(f"{key}={value}" for key, value in params.items()))
return response.json()

def get_page(self) -> FacebookPage:
"""
Get facebook page
"""
page_dict = self._api_client.get_object(
self._page_id, fields="link,name,followers_count,fan_count"
)
page_dict = self._make_graph_api_call(str(self._page_id), {
'fields': 'link,name,followers_count,fan_count'
})
return FacebookPage.from_dict(page_dict)

def get_new_posts_count(self, since: datetime, until: datetime) -> int:
Expand Down
2 changes: 1 addition & 1 deletion src/tg/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# Admin (developer) handlers
from .get_roles_for_member_handler import get_roles_for_member
from .get_tasks_report_handler import get_tasks_report
from .get_tasks_report_handler import get_tasks_report, get_tasks_report_advanced
from .help_handler import help
from .list_chats_handler import list_chats
from .list_job_handler import list_jobs
Expand Down
12 changes: 12 additions & 0 deletions src/tg/handlers/access_config_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,25 @@ def reload_config_jobs(update, tg_context):
reply(load("access_config_handler__reload_config_jobs_usage_example"), update)
logger.warning(f"Failed to reload jobs config: {e}")
return
num_strings = 0
try:
num_strings = AppContext().strings_db_client.fetch_strings_sheet(
AppContext().sheets_client
)
except Exception as e:
reply(load("access_config_handler__reload_config_jobs_usage_example"), update)
logger.warning(f"Failed to reload jobs config when fetching strings: {e}")
reply(
load(
"common__code_wrapper",
arg=json.dumps(ConfigManager.redact(config_jobs), indent=2),
),
update,
)
reply(
load("db_fetch_strings_sheet_job__success", num_strings=num_strings),
update,
)


@admin_only
Expand Down
21 changes: 18 additions & 3 deletions src/tg/handlers/get_tasks_report_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@

@manager_only
def get_tasks_report(update: telegram.Update, tg_context: telegram.ext.CallbackContext):
# set initial dialogue data
_get_task_report_base(update, tg_context, advanced=False)

return


@manager_only
def get_tasks_report_advanced(
update: telegram.Update, tg_context: telegram.ext.CallbackContext
):
_get_task_report_base(update, tg_context, advanced=True)

return


def _get_task_report_base(
update: telegram.Update, tg_context: telegram.ext.CallbackContext, advanced: bool
):
app_context = AppContext()

boards_list = app_context.trello_client.get_boards_for_user()
Expand All @@ -34,15 +50,14 @@ def get_tasks_report(update: telegram.Update, tg_context: telegram.ext.CallbackC
tg_context.chat_data[TASK_NAME] = {
consts.NEXT_ACTION: consts.PlainTextUserAction.GET_TASKS_REPORT__ENTER_BOARD_NUMBER.value
}
tg_context.chat_data["advanced"] = advanced
reply(
load(
"get_tasks_report_handler__choose_trello_board", lists=boards_list_formatted
),
update,
)

return


def generate_report_messages(
board_id: str, list_id: str, introduction: str, add_labels: bool
Expand Down
42 changes: 28 additions & 14 deletions src/tg/handlers/user_message_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def handle_user_message(
assert 0 <= list_idx < len(board_list)
board_id = board_list[list_idx]["id"]
trello_lists = trello_client.get_lists(board_id)
trello_lists = trello_lists[::-1]
except Exception as e:
logger.warning(e)
reply(
Expand All @@ -107,7 +108,10 @@ def handle_user_message(
]

trello_lists_formatted = "\n".join(
[f"{i + 1}) {lst.name}" for i, lst in enumerate(trello_lists)]
[
f"{len(trello_lists) - i}) {lst.name}"
for i, lst in enumerate(trello_lists)
]
)
reply(
load(
Expand All @@ -123,8 +127,8 @@ def handle_user_message(
elif next_action == PlainTextUserAction.GET_TASKS_REPORT__ENTER_LIST_NUMBER:
try:
trello_lists = command_data.get(consts.GetTasksReportData.LISTS, [])
list_idx = int(user_input) - 1
assert 0 <= list_idx < len(trello_lists)
list_idx = -int(user_input)
assert 0 > list_idx >= -len(trello_lists)
list_id = trello_lists[list_idx]["id"]
except Exception as e:
logger.warning(e)
Expand All @@ -148,6 +152,12 @@ def handle_user_message(
]
]
)
if not tg_context.chat_data.get("advanced"):
add_labels = button == ButtonValues.GET_TASKS_REPORT__LABELS__NO
command_data[consts.GetTasksReportData.INTRO_TEXT] = None
handle_task_report(command_data, add_labels, update)
return

reply(
load("get_tasks_report_handler__enter_intro"),
update,
Expand Down Expand Up @@ -188,17 +198,7 @@ def handle_user_message(
reply(load("user_message_handler__press_button_please"), update)
return
add_labels = button == ButtonValues.GET_TASKS_REPORT__LABELS__YES
board_id = command_data[consts.GetTasksReportData.BOARD_ID]
list_id = command_data[consts.GetTasksReportData.LIST_ID]
introduction = command_data[consts.GetTasksReportData.INTRO_TEXT]
messages = get_tasks_report_handler.generate_report_messages(
board_id, list_id, introduction, add_labels
)
for message in messages:
reply(message, update)
# finished with last action for /trello_client_get_lists
set_next_action(command_data, None)
return
handle_task_report(command_data, add_labels, update)
elif next_action == PlainTextUserAction.MANAGE_REMINDERS__CHOOSE_ACTION:
if button is None:
reply(load("user_message_handler__press_button_please"), update)
Expand Down Expand Up @@ -546,3 +546,17 @@ def handle_new_members(
# writes chat_id and chat name to db when anybody (including the bot) is added to a new chat
# very heuristic solution
DBClient().set_chat_name(get_chat_id(update), get_chat_name(update))


def handle_task_report(command_data, add_labels, update):
board_id = command_data[consts.GetTasksReportData.BOARD_ID]
list_id = command_data[consts.GetTasksReportData.LIST_ID]
introduction = command_data[consts.GetTasksReportData.INTRO_TEXT]
messages = get_tasks_report_handler.generate_report_messages(
board_id, list_id, introduction, add_labels
)
for message in messages:
reply(message, update)
# finished with last action for /trello_client_get_lists
set_next_action(command_data, None)
return

0 comments on commit be7889a

Please sign in to comment.