async def pingme(message: Message): """ ping tg servers """ start = datetime.now() await message.client.send(Ping(ping_id=0)) end = datetime.now() m_s = (end - start).microseconds / 1000 await message.edit(f"**Pong!**\n`{m_s} ms`")
async def ping_func(answers): t1 = time() ping = Ping(ping_id=randint(696969, 6969696)) await app.send(ping) t2 = time() ping = f"{str(round((t2 - t1), 6) * 100)} ms" answers.append( InlineQueryResultArticle( title=ping, input_message_content=InputTextMessageContent(f"__**{ping}**__"))) return answers
async def ping_cmd(client, message): """a sunny day! The ping command """ before = time.time() await client.send(Ping(ping_id=69)) after = time.time() latency = (after - before) * 1000 answer = "a sunny day" if message.command.base == "asd" else "pong" await edit_or_reply(message, f"` → ` {answer} (`{latency:.1f}` ms)")
async def ping_func(answers): ping = Ping(ping_id=app.rnd_id()) t1 = time() await app.send(ping) t2 = time() ping = f"{str(round((t2 - t1) * 1000, 2))} ms" answers.append( InlineQueryResultArticle( title=ping, input_message_content=InputTextMessageContent(f"__**{ping}**__"), )) return answers
async def info_cmd(client, message): """info about project and client status Will show project version+link, current uptime, session age (for users), latency, plugins count and system+load specs. The project version is composed as v `release` - `commit_hash` , to identify down to the single commit executing code. Hardware specs might not be super accurate, especially on virtualized hosts. Process CPU usage is calculated as `CPU_TIME / EXECUTION_TIME`. cpu_time includes both user mode and kernel mode. CPU usage is thus an average usage across the entirety of the process execution. CPU usage will count both main process and any child (still executing or terminated). System total CPU usage instead is calculated as a delta since last call. It is a hardly significant value since it fluctuates a lot. RAM total and used is calculated appropriately both for process and for system. """ before = time.time() await client.send(Ping(ping_id=69)) after = time.time() latency = (after - before) * 1000 self_proc = psutil.Process(os.getpid()) ram_usage = self_proc.memory_percent() ram_load = psutil.virtual_memory().percent total_ram = psutil.virtual_memory().total # cpu_usage = self_proc.cpu_percent() # getting it like this always returns 0 # # let's instead calculate CPU usage as cpu_time/execution_time execution_time = (datetime.now() - client.start_time).total_seconds() res_self = resource.getrusage(resource.RUSAGE_SELF) res_child = resource.getrusage(resource.RUSAGE_CHILDREN) cpu_time = res_self.ru_utime + res_child.ru_utime + res_self.ru_stime + res_child.ru_stime cpu_usage = cpu_time / execution_time cpu_load = psutil.cpu_percent() # total cpu_count = psutil.cpu_count() cpu_freq = max(psutil.cpu_freq().max, psutil.cpu_freq().current ) # max might be 0 and current might be lower than max with open(".gitmodules") as f: # not too nice but will do for now plugin_count = f.read().count("[submodule") if not client.me.is_bot: sess = list( filter(lambda x: x.current, (await client.send(GetAuthorizations())).authorizations))[0] session_age = datetime.now() - datetime.utcfromtimestamp( sess.date_created) await edit_or_reply( message, f'<code>→ </code> <a href="https://github.com/alemidev/alemibot">ᚨᛚᛖᛗᛁᛒᛟᛏ</a> <code>v{client.app_version}</code>\n' + f"<code> → </code> <b>uptime</b> <code>{str(datetime.now() - client.start_time)}</code>\n" + (f"<code> → </code> <b>session age</b> <code>{str(session_age)}</code>\n" if not client.me.is_bot else "") + f"<code> → </code> <b>latency</b> <code>{latency:.1f} ms</code>\n" + f"<code> → </code> <b>plugins</b> <code>{plugin_count}</code>\n" + f"<code>→ </code> <b>system</b> <code>{platform.system()}-{platform.machine()} {platform.release()}</code>\n" + f"<code> → </code> <b>cpu [</b><code>{cpu_count}x {cpu_freq/1000:.1f}GHz</code><b>] load</b> <code>{cpu_usage:.2f}%</code> (<code>{cpu_load:.2f}%</code> system)\n" + f"<code> → </code> <b>mem [</b><code>{order_suffix(total_ram)}</code><b>] use</b> <code>{ram_usage:.2f}%</code> (<code>{ram_load:.2f}%</code> system)\n" + f"<code>→ </code> <b>python</b> <code>{platform.python_version()}</code>\n", parse_mode="html", disable_web_page_preview=True)