Skip to content

Commit

Permalink
[FEAT] Object Payload sent in action (#54)
Browse files Browse the repository at this point in the history
- set_action can receive Payload Object
- action decorator can receive data send in payload object
- Unit test for Payload in action added
  • Loading branch information
gaetan1903 authored Aug 29, 2022
1 parent 296c2f1 commit 6d4bb1c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
3 changes: 3 additions & 0 deletions ampalibe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ async def main(request: Request):
os.remove(f"assets/private/.__{sender_id}")

payload, kw = Payload.trt_payload_in(payload)
if action:
action, kw_tmp = Payload.trt_payload_in(action)
kw.update(kw_tmp)
command = funcs["command"].get(payload.split()[0])
kw["sender_id"] = sender_id
kw["cmd"] = payload
Expand Down
6 changes: 4 additions & 2 deletions ampalibe/messenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ def __analyse(self, res, log_level="error"):

@property
def token(self):
if self.access_token.strip() == "":
print("Warning! EMPTY PAGE ACCESS TOKEN", file=stderr)
if not self.access_token:
print(
"\033[48:5:166m⚠ Warning!\033[0m EMPTY PAGE ACCESS TOKEN", file=stderr
)
return self.access_token

@retry(requests.exceptions.ConnectionError, tries=3, delay=3)
Expand Down
10 changes: 7 additions & 3 deletions ampalibe/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from .utils import Payload
from conf import Configuration # type: ignore


Expand Down Expand Up @@ -59,7 +60,7 @@ def __init_db(self):
CREATE TABLE IF NOT EXISTS `amp_user` (
`id` INT NOT NULL AUTO_INCREMENT,
`user_id` varchar(50) NOT NULL UNIQUE,
`action` varchar(50) DEFAULT NULL,
`action` TEXT DEFAULT NULL,
`last_use` datetime NOT NULL DEFAULT current_timestamp(),
`lang` varchar(5) DEFAULT NULL,
`tmp` varchar(255) DEFAULT NULL,
Expand All @@ -71,7 +72,7 @@ def __init_db(self):
CREATE TABLE IF NOT EXISTS "amp_user" (
id SERIAL,
user_id VARCHAR NULL DEFAULT NULL,
action VARCHAR NULL DEFAULT NULL,
action TEXT NULL DEFAULT NULL,
tmp VARCHAR NULL DEFAULT NULL,
last_use TIMESTAMP NULL DEFAULT NOW(),
lang VARCHAR NULL DEFAULT NULL,
Expand Down Expand Up @@ -167,9 +168,12 @@ def set_action(self, user_id, action):
"""
define a current action if an user
@params : user_id
@params : user_id, action
@return: None
"""
if isinstance(action, Payload):
action = Payload.trt_payload_out(action)

if self.ADAPTER == "MYSQL" or self.ADAPTER == "POSTGRESQL":
req = "UPDATE amp_user set action = %s WHERE user_id = %s"
else:
Expand Down
13 changes: 13 additions & 0 deletions automate/app/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ def action_work(sender_id, cmd, **extends):
if query.get_temp(sender_id, "myname") != myname:
return cmd + " " + myname
return "Del temp error"


@ampalibe.command("/try_second_action")
def try_second_action(sender_id, **extends):
query.set_action(
sender_id, Payload("/second_action_work", myname="Ampalibe", version=2)
)


@ampalibe.action("/second_action_work")
def second_action_work(sender_id, cmd, myname, version, **extends):
query.set_action(sender_id, None)
return cmd + " " + myname + str(version)
11 changes: 11 additions & 0 deletions automate/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,16 @@ else
exit 1
fi

####### ACTION TEST && Payload data TEST #######
simulate '"/try_second_action"' > /dev/null
res=$(simulate '"Hello"')
if [ "$res" = '"Hello Ampalibe2"' ]; then
echo "Message: OK Second Action"
echo "Message: OK Payload data in action"
else
echo KO
exit 1
fi



0 comments on commit 6d4bb1c

Please sign in to comment.