Skip to content

Commit c8e6340

Browse files
committed
update advanced example
1 parent a22ff4b commit c8e6340

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

examples/echobot_advanced.py

+27-18
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,39 @@
55
cli = BotCli("echobot")
66

77

8+
@cli.on_init
9+
def on_init(bot, args):
10+
bot.logger.info("Initializing CLI with args: %s", args)
11+
for accid in bot.rpc.get_all_account_ids():
12+
if not bot.rpc.get_config(accid, "displayname"):
13+
bot.rpc.set_config(accid, "displayname", "EchoBot")
14+
status = "I am a Delta Chat bot, I will repeat anything you say to me"
15+
bot.rpc.set_config(accid, "selfstatus", status)
16+
bot.rpc.set_config(accid, "delete_server_after", "1") # delete immediately from server
17+
# bot.rpc.set_config(accid, "delete_device_after", "3600")
18+
19+
20+
@cli.on_start
21+
def on_start(bot, _args):
22+
bot.logger.info(
23+
"Bot is listening to requests... here connect to databases, start worker threads, etc"
24+
)
25+
26+
827
@cli.on(events.RawEvent)
9-
def log_event(bot, _accid, event):
28+
def log_event(bot, accid, event):
1029
if event.kind == EventType.INFO:
11-
bot.logger.info(event.msg)
30+
bot.logger.debug(event.msg)
1231
elif event.kind == EventType.WARNING:
1332
bot.logger.warning(event.msg)
1433
elif event.kind == EventType.ERROR:
1534
bot.logger.error(event.msg)
35+
elif event.kind == EventType.SECUREJOIN_INVITER_PROGRESS:
36+
if event.progress == 1000:
37+
# bot's QR scanned by an user, send introduction message
38+
chatid = bot.rpc.create_chat_by_contact_id(accid, event.contact_id)
39+
reply = {"text": "Hi, I will repeat anything you say to me"}
40+
bot.rpc.send_msg(accid, chatid, reply)
1641

1742

1843
@cli.on(events.NewMessage(is_info=False, func=is_not_known_command))
@@ -27,22 +52,6 @@ def _help(bot, accid, event):
2752
bot.rpc.send_msg(accid, msg.chat_id, {"text": "I will repeat anything you say to me"})
2853

2954

30-
@cli.on_init
31-
def on_init(bot, args):
32-
bot.logger.info("Initializing CLI with args: %s", args)
33-
for accid in bot.rpc.get_all_account_ids():
34-
if not bot.rpc.get_config(accid, "displayname"):
35-
bot.rpc.set_config(accid, "displayname", "EchoBot")
36-
status = "I am a Delta Chat bot, I will repeat anything you say to me"
37-
bot.rpc.set_config(accid, "selfstatus", status)
38-
bot.rpc.set_config(accid, "delete_server_after", "1") # delete immediately from server
39-
40-
41-
@cli.on_start
42-
def on_start(bot, _args):
43-
bot.logger.info("Running bot...")
44-
45-
4655
def test(_cli, bot, args):
4756
"""just some example subcommand"""
4857
bot.logger.info("Hello %s, this is an example subcommand!", args.name)

0 commit comments

Comments
 (0)