add a test echo_and_quit examples

This commit is contained in:
holger krekel
2020-03-03 18:55:19 +01:00
parent 91cdc76414
commit f61b9f7964
8 changed files with 106 additions and 57 deletions

View File

@@ -1,37 +1,57 @@
# instantiate and configure deltachat account
# content of echo_and_quit.py
import sys
import optparse
import deltachat
ac = deltachat.Account("/tmp/db")
# to see low-level events in the console uncomment the following line
# ac.add_account_plugin(deltachat.eventlogger.FFIEventLogger(ac, ""))
if not ac.is_configured():
ac.set_config("addr", "tmpy.94mtm@testrun.org")
ac.set_config("mail_pw", "5CbD6VnjD/li")
ac.set_config("mvbox_watch", "0")
ac.set_config("sentbox_watch", "0")
class MyPlugin:
class SimpleEchoPlugin:
@deltachat.hookspec.account_hookimpl
def process_incoming_message(self, message):
print("process_incoming message", message)
if message.text.strip() == "/quit":
print("shutting down")
ac.shutdown()
message.account.shutdown()
else:
ch = ac.create_chat_by_contact(message.get_sender_contact())
ch.send_text("echoing {}".format(message.text))
ch = message.account.create_chat_by_contact(message.get_sender_contact())
ch.send_text("echoing from {}:\n{}".format(message.get_sender_contact().addr, message.text))
@deltachat.hookspec.account_hookimpl
def process_message_delivered(self, message):
print("process_message_delivered", message)
ac.add_account_plugin(MyPlugin())
# start IO threads and perform configuration
ac.start()
def main(argv):
p = optparse.OptionParser("simple-echo")
p.add_option("-l", action="store_true", help="show low-level events")
p.add_option("--db", type="str", help="database file")
p.add_option("--email", type="str", help="email address")
p.add_option("--password", type="str", help="password")
print("waiting for /quit or other message on {}".format(ac.get_config("addr")))
opts, posargs = p.parse_args(argv)
ac.wait_shutdown()
assert opts.db, "you must specify --db"
ac = deltachat.Account(opts.db)
if opts.l:
ac.add_account_plugin(deltachat.eventlogger.FFIEventLogger(ac, "echo"))
if not ac.is_configured():
assert opts.email and opts.password, "you must specify --email and --password"
ac.set_config("addr", opts.email)
ac.set_config("mail_pw", opts.password)
ac.set_config("mvbox_watch", "0")
ac.set_config("sentbox_watch", "0")
ac.add_account_plugin(SimpleEchoPlugin())
# start IO threads and perform configure if neccessary
ac.start(callback_thread=True)
print("waiting for /quit or message to echo on: {}".format(ac.get_config("addr")))
ac.wait_shutdown()
if __name__ == "__main__":
main(sys.argv)