Commit bc2c2204 authored by Szymon Zimnowoda's avatar Szymon Zimnowoda
Browse files

wip integration test

parent 8d61b080
Pipeline #11635 passed with stages
in 5 minutes and 14 seconds
Showing with 67 additions and 6 deletions
+67 -6
......@@ -15,7 +15,7 @@ classifiers =
packages = find:
python_requires = >=3.8
install_requires =
pymemri @ git+https://gitlab.memri.io/memri/pymemri.git@v0.0.30
pymemri @ git+https://gitlab.memri.io/memri/pymemri.git@dev
pytest
markdown-it-py
telethon @ git+https://github.com/LonamiWebs/Telethon.git@v1
......
......@@ -23,6 +23,7 @@ from telethon.crypto.authkey import AuthKey
from .utils import extract_peer_id_as_str, get_external_ids, get_item_by_external_id, generate_qr_svg, get_me
from .constants import SERVICE_NAME, BULK_ACTION_SIZE, FILE_UPLOAD_BATCH_SIZE
# App credentials
APP_KEY = os.environ["TELEGRAM_APP_KEY"] if "TELEGRAM_APP_KEY" in os.environ else None
APP_SECRET = os.environ["TELEGRAM_APP_SECRET"] if "TELEGRAM_APP_SECRET" in os.environ else None
......@@ -40,7 +41,7 @@ class TelegramPlugin(PluginBase):
running = True
writing = False
me_id = None
def __init__(self, **kwargs):
super().__init__(**kwargs)
......@@ -101,7 +102,7 @@ class TelegramPlugin(PluginBase):
user_id = str(user.id)
if user_id == self.me_id or user.deleted:
return
if not self.exists(user_id, itemType):
items = []
updates = []
......@@ -203,6 +204,9 @@ class TelegramPlugin(PluginBase):
logger.info(f"Using existing account: {self.me.id} {self.me.displayName} {self.me.externalId}")
try:
session_dict = json.loads(self.me.secret)
logger.info(f"session dict {session_dict}")
session = sessions.MemorySession()
session.set_dc(session_dict['dc_id'], session_dict['server_address'], session_dict['port'])
session.takeout_id = session_dict['takeout_id']
......@@ -276,26 +280,33 @@ class TelegramPlugin(PluginBase):
self.set_progress(min(0.99, round((self.pluginRun.progress + increment)*100)/100))
async def startTelegram(self):
logger.info("1")
self.set_me_up()
# (2) Create the client and connect
client = TelegramClient(self.session, APP_KEY, APP_SECRET)
logger.info("2")
await client.connect()
if not client.is_connected:
raise Exception("Cannot connect to Telegram")
logger.info("3")
await self.authenticate(client)
# load data required for preventing duplication
self.init_existing_ids()
# allow sending messages - register message listener task
asyncio.ensure_future(self.send_message_listener(client))
logger.info("4")
# start incoming message listener
client.add_event_handler(self.message_handler, events.NewMessage)
# fetch with me information
me = await client.get_me()
logger.info("5")
self.process_me(me, client.session)
await self.process_entity_photo(client, me)
# Start importing
# Start importing
await self.import_dialogs(client)
# Wrap up
self.write()
......@@ -367,7 +378,7 @@ class TelegramPlugin(PluginBase):
else:
raise ValueError(
f"Could not send message, no receiver defined, or receiver has no phoneNumber {msg.receiver}, {msg.externalId}")
async def send_message_listener(self, client):
logger.info("Send loop started")
......@@ -390,6 +401,6 @@ class TelegramPlugin(PluginBase):
logger.warning(f"failed to send message:\n{e}")
msg.sendStatus = "sendFailed"
self.client.update_item(msg)
def add_to_schema(self):
self.client.add_to_schema(Account, Message, MessageChannel, Photo)
import json
import os
import threading
from time import sleep
import pytest
from pymemri.pod.client import PodClient
from telegram.plugin import TelegramPlugin
......@@ -62,3 +66,49 @@ def test_plugin():
assert accounts[0].externalId == "12345"
from pymemri.data.schema import PluginRun
from pymemri.plugin.pluginbase import PluginBase
from pymemri.plugin.pluginbase import run_plugin_from_run_id
def launch_plugin_under_test(plugin: PluginBase):
def start_plugin():
plugin.add_to_schema()
plugin._run()
threading.Thread(target=start_plugin)
def test_plugin_integration():
# {
# "database_key": "7902549546614598853752921053159845817328810955557383118823980729",
# "owner_key": "7029450735042878689201489007441979844895302333395213346946316360"
# }%
# TELEGRAM_APP_KEY=22945652
# TELEGRAM_APP_SECRET=6fc647b6f93c8c8250d6bf7448d1db27
os.environ["TELEGRAM_APP_KEY"] = "22945652"
os.environ["TELEGRAM_APP_SECRET"] = "6fc647b6f93c8c8250d6bf7448d1db27"
run = PluginRun(
containerImage="",
pluginModule="telegram.plugin",
pluginName="TelegramPlugin",
status="not started",
)
client = PodClient(database_key="7902549546614598853752921053159845817328810955557383118823980729",
owner_key="7029450735042878689201489007441979844895302333395213346946316360", create_account=False)
client.create(run)
plugin = TelegramPlugin(client=client, pluginRun=run)
print("running plugin")
launch_plugin_under_test(plugin)
print("plugin launched")
sleep(10)
pass
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment