Skip to content

Commit

Permalink
f/check (WIP) people around + chore(doc) updates (#9)
Browse files Browse the repository at this point in the history
* feat(checks): implem in wip

* feat: small update on DOC

* release ready for the alpha

* fix: test
  • Loading branch information
Sanix-Darker authored Apr 29, 2024
1 parent d88c1e3 commit de2140e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 23 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
## TCHAKA

This bot is a simple telegram bot that makes you in contacts with people
'around you anonymously'.
This bot makes you in contact with people 'around' you anonymously based on
your localisation.

No DATA saved + all IN MEMORY...

## HOW GET IT RUN

Expand All @@ -13,7 +15,7 @@ $ cp .env.example .env

$ docker build -t tchaka:latest -f ./Dockerfile .
$ docker run -ti tchaka
INFO:__main__:tchaka v0.0.1 started successfully...
INFO:__main__:tchaka started successfully...
INFO:telegram.ext.Application:Application started
```

Expand All @@ -29,7 +31,7 @@ $ make install
$ make run
# to start the bot...
python -m tchaka.main
INFO:__main__:tchaka v0.0.1 started successfully...
INFO:__main__:tchaka started successfully...
INFO:telegram.ext.Application:Application started
```

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "tchaka"
version = "0.0.3"
version = "0.1.0-alpha"
description = "Euphemeral chat based on the position"
authors = ["sanix-darker <[email protected]>"]
license = "Apache"
Expand Down
18 changes: 14 additions & 4 deletions tchaka/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,23 @@ async def start_callback(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None

async def check_callback(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
"""
TODO: not working yet, will be fixed in future version
CheckCallBack to check how many people are in the area
"""

_, message = await get_user_and_message(update)

await append_chat_ids_messages(message.chat_id, message.message_id)

if not (given_user_name := _CHAT_IDS.get(message.chat_id)):
given_user_name = "New User"
await message.reply_text(
text="Send your localisation to be put in a group first please"
)
return

await append_chat_ids_messages(message.chat_id, message.message_id)
await message.reply_text(text="---")
await message.reply_text(text="There is ---")
_LOGGER.info(f"/start :: {given_user_name=}")


Expand Down Expand Up @@ -173,7 +179,11 @@ async def location_callback(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> N
user_new_name = await build_user_hash(user.full_name)

await append_chat_ids_messages(message.chat_id, message.message_id)
_USERS, _GROUPS = await populate_new_user_to_appropriate_group(
(
_USERS,
_GROUPS,
count_user_same_group,
) = await populate_new_user_to_appropriate_group(
user_new_name=user_new_name,
current_chat_id=message.chat_id,
latitude=location.latitude,
Expand All @@ -195,7 +205,7 @@ async def location_callback(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> N
text=html_format_text(
build_welcome_location_message_for_current_user(
user_new_name,
_USERS,
count_user_same_group,
user.language_code or "en",
)
)
Expand Down
2 changes: 1 addition & 1 deletion tchaka/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"fr": {
"WELCOME_MESSAGE": """Bienvenue sur Tchaka!
Commencez par envoyer votre localisation (aucun soucis, c est anonyme et rien
ne se sauvegardes.):""",
ne se sauvegardes)""",
"HELP_MESSAGE": """/start - Pour demarrer.
/help - Comment cela fonctionne.
/stop - Pour stoper le bot et cleaner toutes vos infos.
Expand Down
15 changes: 10 additions & 5 deletions tchaka/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ def haversine_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> fl
async def group_coordinates(
coordinates: list[tuple[float, float]],
distance_threshold: int = 100,
) -> dict[str, list]:
user_coords: tuple[float, float] | None = None,
) -> tuple[dict[str, list], int]:
"""
Group coordinates based on their proximity within a certain distance threshold.
Returns a dictionary with group IDs as keys and lists of coordinates as values.
"""

users_in_same_group = 0
groups: dict[str, list] = {}
for coord in coordinates:
group_found = False
Expand All @@ -59,11 +61,13 @@ async def group_coordinates(
):
groups[group_id].append(coord)
group_found = True
if user_coords and user_coords in groups[group_id]:
users_in_same_group = len(groups[group_id])
break
if not group_found:
groups[f"___G-{len(groups)+1}"] = [coord]

return groups
return groups, users_in_same_group


async def dispatch_msg_in_group(
Expand Down Expand Up @@ -177,19 +181,20 @@ async def populate_new_user_to_appropriate_group(
longitude: float,
user_list: dict[str, Any],
group_list: dict[str, Any],
) -> tuple[dict[str, Any], dict[str, Any]]:
) -> tuple[dict[str, Any], dict[str, Any], int]:
"""
This method set the user infos and put him in a group
"""

user_list[user_new_name] = [current_chat_id, (latitude, longitude)]
group_list = await group_coordinates(
group_list, user_same_group = await group_coordinates(
coordinates=[user_info[1] for user_info in user_list.values()],
distance_threshold=100,
user_coords=(latitude, longitude),
)

return user_list, group_list
return user_list, group_list, user_same_group


async def clean_all_msg(
Expand Down
11 changes: 5 additions & 6 deletions tchaka/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from functools import lru_cache
from typing import Any
from telegram import Message, Update, User
import logging
import html
Expand Down Expand Up @@ -57,7 +56,7 @@ async def build_user_hash(fullname: str) -> str:

def build_welcome_location_message_for_current_user(
user_new_name: str,
users_list: dict[str, Any],
users_list_count: int,
lang: str,
) -> str:
"""
Expand All @@ -68,11 +67,11 @@ def build_welcome_location_message_for_current_user(
if lang == "fr":
suggest_to_connect = (
(
f"Il y a ({len(users_list)-1}) personnes dans la meme zone que "
f"Il y a ({users_list_count}) personnes dans la meme zone que "
"vous. ils sont avertis.\n"
"N'hesitez pas a dire bonjour.\n"
)
if len(users_list) > 1
if users_list_count > 1
else ("0 utilisateurs ici pour le moment.\n")
)
final_msg = (
Expand All @@ -85,11 +84,11 @@ def build_welcome_location_message_for_current_user(
else:
suggest_to_connect = (
(
f"There is ({len(users_list)-1}) people in the same area than "
f"There is ({users_list_count}) people in the same area than "
"you. They just get notified.\n"
"Feel free to say hi.\n"
)
if len(users_list) > 1
if users_list_count > 1
else ("0 users here for now.\n")
)
final_msg = (
Expand Down
5 changes: 3 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async def test_group_coordinates():
(2.8205, 4.0055),
(52.5200, 13.4060),
]
grouped_coordinates = await group_coordinates(
grouped_coordinates, count_u = await group_coordinates(
coordinates,
distance_threshold=100,
)
Expand Down Expand Up @@ -113,7 +113,7 @@ async def test_populate_new_user_to_appropriate_group(
mocker: MockType,
) -> None:
mocker.patch(
"tchaka.core.group_coordinates", return_value={"group1": [(50.0, 60.0)]}
"tchaka.core.group_coordinates", return_value=({"group1": [(50.0, 60.0)]}, 1)
)

new_user_name = "user1"
Expand All @@ -124,6 +124,7 @@ async def test_populate_new_user_to_appropriate_group(
(
updated_user_list,
updated_group_list,
count_u,
) = await populate_new_user_to_appropriate_group(
new_user_name, current_chat_id, latitude, longitude, {}, {}
)
Expand Down

0 comments on commit de2140e

Please sign in to comment.