generated from cheshire-cat-ai/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeowgram_connect.py
63 lines (45 loc) · 1.89 KB
/
meowgram_connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
from typing import Dict
from cat.mad_hatter.mad_hatter import MadHatter
from cat.mad_hatter.decorators import hook
from .settings import NameType
def get_name(telegram_update):
settings = MadHatter().get_plugin().load_settings()
name = telegram_update["message"]["from"].get("first_name", None)
username = telegram_update["message"]["from"].get("username", None)
if settings["name_to_use"] == NameType.NAME.value and name:
return name
elif settings["name_to_use"] == NameType.USERNAME.value and username:
return username
return None
@hook
def after_cat_recalls_memories(cat) -> Dict:
user_message = cat.working_memory.user_message_json
# This key exist only if the message is from Meowgram
# if not exists we don't need to do anything
if "meowgram" not in user_message.keys():
return
# Get the name from the telegram update
telegram_update = json.loads(user_message["meowgram"]["update"])
name = get_name(telegram_update)
# Update name in chat history
cat.working_memory.history[-1]["who"] = name
@hook
def before_cat_sends_message(message, cat):
settings = cat.mad_hatter.get_plugin().load_settings()
user_message = cat.working_memory["user_message_json"]
# This key exist only if the message is from Meowgram
# if not exists we don't need to do anything
if "meowgram" not in user_message.keys():
return
telegram_update = json.loads(user_message["meowgram"]["update"])
message["meowgram"] = {
"send_params": {},
"settings": {},
}
send_params = message["meowgram"]["send_params"]
meowgram_settings = message["meowgram"]["settings"]
if settings["reply_to"]:
send_params["reply_to_message_id"] = telegram_update["message"]["message_id"]
meowgram_settings["show_tts_text"] = settings["show_tts_text"]
return message