This repository has been archived by the owner on Oct 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplebot_howdoi.py
64 lines (47 loc) · 1.75 KB
/
simplebot_howdoi.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
64
"""Plugin's filters and commands definitions."""
import json
import simplebot
from deltachat import Message
from howdoi.howdoi import howdoi
from pkg_resources import DistributionNotFound, get_distribution
from simplebot.bot import Replies
try:
__version__ = get_distribution(__name__).version
except DistributionNotFound:
# package is not installed
__version__ = "0.0.0.dev0-unknown"
@simplebot.filter
def query_filter(message: Message, replies: Replies) -> None:
"""Send me a question in private to get an answer."""
if not message.chat.is_multiuser():
_search(message.text, replies)
@simplebot.command(name="/howdoi")
def cmd_howdoi(payload: str, replies: Replies) -> None:
"""Instant coding answers.
Example:
/howdoi format date bash
You can also send me the question direclty in private.
"""
_search(payload, replies)
def _search(query: str, replies: Replies) -> None:
try:
resp = json.loads(howdoi(f"{query} -j"))[0]
if resp["answer"]:
replies.add(text=f"{resp['answer']}\n\n↗️ {resp['link']}")
else:
replies.add(text="❌ Nothing found.")
except (Exception, SystemExit): # noqa
replies.add(text="❌ Something went wrong.")
class TestPlugin:
"""Online tests"""
def test_howdoi(self, mocker):
msg = mocker.get_one_reply("/howdoi format date bash")
assert "↗" in msg.text
msg = mocker.get_one_reply("/howdoi -h")
assert "❌" in msg.text
def test_filter(self, mocker):
msg = mocker.get_one_reply("format date bash")
assert "↗" in msg.text
# filter should work only in private/direct chat
msgs = mocker.get_replies("format date bash", group="group1")
assert not msgs