-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathecho_bot.py
49 lines (40 loc) · 1.69 KB
/
echo_bot.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
#!/usr/bin/env python3
"""
Example echo bot without using hooks
"""
import logging
import sys
import os
from deltachat_rpc_client import DeltaChat, EventType, Rpc, SpecialContactId
def main():
with Rpc() as rpc:
deltachat = DeltaChat(rpc)
system_info = deltachat.get_system_info()
logging.info("Running deltachat core %s", system_info["deltachat_core_version"])
accounts = deltachat.get_all_accounts()
account = accounts[0] if accounts else deltachat.add_account()
account.set_config("bot", "1")
if not account.is_configured():
logging.info("Account is not configured, configuring")
account.set_config("addr", os.environ['MAIL_ADDR'])
account.set_config("mail_pw", os.environ['MAIL_PW'])
account.configure()
logging.info("Configured")
else:
logging.info("Account is already configured")
deltachat.start_io()
while True:
event = account.wait_for_event()
if event["kind"] == EventType.INFO:
logging.info("%s", event["msg"])
elif event["kind"] == EventType.WARNING:
logging.warning("%s", event["msg"])
elif event["kind"] == EventType.ERROR:
logging.error("%s", event["msg"])
elif event["kind"] == EventType.INCOMING_MSG:
snapshot = account.get_message_by_id(event["msg_id"]).get_snapshot()
if snapshot.from_id != SpecialContactId.SELF and not snapshot.is_bot and not snapshot.is_info:
snapshot.chat.send_text(snapshot.text)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()