xtaothon/docs/api/handlers.html

324 lines
16 KiB
HTML
Raw Normal View History

2021-06-11 12:08:29 +00:00
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh-CN" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh-CN" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>消息更新处理器 &mdash; Xtaothon Documentation</title>
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<link rel="canonical" href="https://docs.xtaolabs.com/api/handlers.html"/>
<script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/clipboard.min.js"></script>
<script src="../_static/copybutton.js"></script>
<script src="../_static/translations.js"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/copybutton.css" type="text/css" />
<link rel="stylesheet" href="../_static/tabs.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="next" title="SOCKS5 Proxy" href="../topics/proxy.html" />
<link rel="prev" title="message.unpin()" href="bound-methods/message.unpin.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html">
<img src="../_static/xtaothon.png" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">简介</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/quickstart.html">快速开始</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/install.html">安装指南</a></li>
</ul>
<p class="caption"><span class="caption-text">开始</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../start/auth.html">授权</a></li>
<li class="toctree-l1"><a class="reference internal" href="../start/invoking.html">调用方法</a></li>
<li class="toctree-l1"><a class="reference internal" href="../start/updates.html">处理消息</a></li>
</ul>
<p class="caption"><span class="caption-text">API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="client.html">客户端</a></li>
<li class="toctree-l1"><a class="reference internal" href="methods/index.html">可用方法</a></li>
<li class="toctree-l1"><a class="reference internal" href="types/index.html">可用类型</a></li>
<li class="toctree-l1"><a class="reference internal" href="bound-methods/index.html">绑定方法</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">消息更新处理器</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#id3">索引</a></li>
<li class="toctree-l2"><a class="reference internal" href="#id4">详细信息</a></li>
</ul>
</li>
</ul>
<p class="caption"><span class="caption-text">主题指南</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../topics/proxy.html">SOCKS5 Proxy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/text-formatting.html">文本格式</a></li>
</ul>
<p class="caption"><span class="caption-text">更多</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">FAQ</a></li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">常见短语</a></li>
<li class="toctree-l1"><a class="reference internal" href="../support.html">支持 Xtaothon</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">xtaothon</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> &raquo;</li>
<li>消息更新处理器</li>
<li class="wy-breadcrumbs-aside">
<!-- User defined GitHub URL -->
<a href="https://github.com/Xtao-Labs/docs-all" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="id1">
<h1>消息更新处理器<a class="headerlink" href="#id1" title="永久链接至标题"></a></h1>
<p>Handlers 用于告诉 Telethon 处理哪种类型的消息更新。</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">telethon.sync</span> <span class="kn">import</span> <span class="n">TelegramClient</span><span class="p">,</span> <span class="n">events</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Client</span><span class="p">(</span><span class="s2">&quot;my_account&quot;</span><span class="p">,</span> <span class="n">api_id</span><span class="p">,</span> <span class="n">api_hash</span><span class="p">)</span>
<span class="k">async</span> <span class="k">def</span> <span class="nf">handler</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
<span class="hll"><span class="n">app</span><span class="o">.</span><span class="n">add_event_handler</span><span class="p">(</span><span class="n">handler</span><span class="p">,</span> <span class="n">events</span><span class="o">.</span><span class="n">NewMessage</span><span class="p">(</span><span class="o">**</span><span class="n">args</span><span class="p">))</span>
</span>
<span class="hll"><span class="n">app</span><span class="o">.</span><span class="n">run_until_disconnected</span><span class="p">()</span>
</span></pre></div>
</div>
<div class="contents local topic" id="id2">
<p class="topic-title">目录</p>
<ul class="simple">
<li><p><a class="reference internal" href="#id3" id="id5">索引</a></p></li>
<li><p><a class="reference internal" href="#id4" id="id6">详细信息</a></p></li>
</ul>
</div>
<hr class="docutils" />
<div class="section" id="id3">
<h2>索引<a class="headerlink" href="#id3" title="永久链接至标题"></a></h2>
<table class="hlist"><tr><td><ul class="simple">
<li><p><a class="reference internal" href="#telethon.events.NewMessage" title="telethon.events.NewMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">NewMessage</span></code></a></p></li>
</ul>
</td><td><ul class="simple">
</ul>
</td><td><ul class="simple">
</ul>
</td></tr></table>
</div>
<hr class="docutils" />
<div class="section" id="id4">
<h2>详细信息<a class="headerlink" href="#id4" title="永久链接至标题"></a></h2>
<dl class="py class">
<dt id="telethon.events.NewMessage">
<em class="property"><span class="pre">class</span> </em><code class="sig-prename descclassname"><span class="pre">telethon.events.</span></code><code class="sig-name descname"><span class="pre">NewMessage</span></code><a class="headerlink" href="#telethon.events.NewMessage" title="永久链接至目标"></a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">chats=None,</span> <span class="pre">incoming=None,</span> <span class="pre">outgoing=None,</span> <span class="pre">from_users=None,</span> <span class="pre">forwards=None,</span> <span class="pre">pattern=None</span></code></p>
<p>消息处理程序类。用于处理来自任何对话(私聊、群组、频道)的文本,媒体消息。
它需要与 <code class="xref py py-meth docutils literal notranslate"><span class="pre">add_handler()</span></code> 一起使用。</p>
<dl>
<dt>参数:</dt><dd><dl class="simple">
<dt>incoming (<code class="docutils literal notranslate"><span class="pre">bool</span></code><code class="docutils literal notranslate"><span class="pre">可选</span></code>):</dt><dd><p>如果设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code> ,则只能处理接收到的消息。与 <code class="docutils literal notranslate"><span class="pre">outgoing</span></code> 相互排斥(只能设置一个 <code class="docutils literal notranslate"><span class="pre">False</span></code>)。</p>
</dd>
<dt>outgoing (<code class="docutils literal notranslate"><span class="pre">bool</span></code><code class="docutils literal notranslate"><span class="pre">可选</span></code>):</dt><dd><p>如果设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code> ,则只能处理此账户通过其他客户端发出的消息。与 <code class="docutils literal notranslate"><span class="pre">incoming</span></code> 相互排斥(只能设置一个 <code class="docutils literal notranslate"><span class="pre">False</span></code>)。</p>
</dd>
<dt>from_users (<code class="xref py py-obj docutils literal notranslate"><span class="pre">User</span></code><code class="docutils literal notranslate"><span class="pre">可选</span></code>):</dt><dd><p>不像 <code class="docutils literal notranslate"><span class="pre">chats</span></code> 参数,此项会筛选出来自此/这些用户的所有消息。如果您只想筛选出来自此/这些用户的私聊对话,请使用 <code class="docutils literal notranslate"><span class="pre">chats</span></code> 参数。</p>
</dd>
<dt>forwards (<code class="docutils literal notranslate"><span class="pre">bool</span></code><code class="docutils literal notranslate"><span class="pre">可选</span></code>):</dt><dd><p>是否处理转发消息。默认情况下,将处理转发和普通消息。如果设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,则只处理转发消息;如果设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code>,则只处理普通消息。</p>
</dd>
<dt>pattern (<code class="docutils literal notranslate"><span class="pre">str</span></code>,``callable``,``Pattern``<code class="docutils literal notranslate"><span class="pre">可选</span></code>):</dt><dd><p>如果设置,只有匹配此模式的消息将被处理。您可以指定 Regex 字符串或者编译的正则表达式模式,如果与消息匹配则将被处理;
如果是可调函数,返回值为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 时消息将被处理。</p>
</dd>
</dl>
</dd>
<dt>Example</dt><dd><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
<span class="kn">from</span> <span class="nn">telethon</span> <span class="kn">import</span> <span class="n">events</span>
<span class="nd">@client</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="n">events</span><span class="o">.</span><span class="n">NewMessage</span><span class="p">(</span><span class="n">pattern</span><span class="o">=</span><span class="s1">&#39;(?i)hello.+&#39;</span><span class="p">))</span>
<span class="k">async</span> <span class="k">def</span> <span class="nf">handler</span><span class="p">(</span><span class="n">event</span><span class="p">):</span>
<span class="c1"># Respond whenever someone says &quot;Hello&quot; and something else</span>
<span class="k">await</span> <span class="n">event</span><span class="o">.</span><span class="n">reply</span><span class="p">(</span><span class="s1">&#39;Hey!&#39;</span><span class="p">)</span>
<span class="nd">@client</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="n">events</span><span class="o">.</span><span class="n">NewMessage</span><span class="p">(</span><span class="n">outgoing</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">pattern</span><span class="o">=</span><span class="s1">&#39;!ping&#39;</span><span class="p">))</span>
<span class="k">async</span> <span class="k">def</span> <span class="nf">handler</span><span class="p">(</span><span class="n">event</span><span class="p">):</span>
<span class="c1"># Say &quot;!pong&quot; whenever you send &quot;!ping&quot;, then delete both messages</span>
<span class="n">m</span> <span class="o">=</span> <span class="k">await</span> <span class="n">event</span><span class="o">.</span><span class="n">respond</span><span class="p">(</span><span class="s1">&#39;!pong&#39;</span><span class="p">)</span>
<span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="k">await</span> <span class="n">client</span><span class="o">.</span><span class="n">delete_messages</span><span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">chat_id</span><span class="p">,</span> <span class="p">[</span><span class="n">event</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">m</span><span class="o">.</span><span class="n">id</span><span class="p">])</span>
</pre></div>
</div>
</dd>
</dl>
</dd></dl>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="../topics/proxy.html" class="btn btn-neutral float-right" title="SOCKS5 Proxy" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="bound-methods/message.unpin.html" class="btn btn-neutral float-left" title="message.unpin()" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(false);
});
</script>
</body>
</html>