<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>telegram &#8211; 天地一沙鸥</title>
	<atom:link href="https://haoluobo.com/tag/telegram/feed/" rel="self" type="application/rss+xml" />
	<link>https://haoluobo.com</link>
	<description>to be continue....</description>
	<lastBuildDate>Thu, 16 Dec 2021 03:12:14 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>
	<item>
		<title>[Telegram Shell Bot]远程执行 Shell 命令的 Telegram 机器人</title>
		<link>https://haoluobo.com/2020/03/telegram-shell-bot/</link>
		
		<dc:creator><![CDATA[vicalloy]]></dc:creator>
		<pubDate>Sun, 01 Mar 2020 11:20:20 +0000</pubDate>
				<category><![CDATA[vicalloy的庄家]]></category>
		<category><![CDATA[telegram]]></category>
		<category><![CDATA[Telegram Shell Bot]]></category>
		<guid isPermaLink="false">/?p=11787</guid>

					<description><![CDATA[项目地址：&#160;https://github.com/vicalloy/telegram-shell-b [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>项目地址：&nbsp;<a href="https://github.com/vicalloy/telegram-shell-bot">https://github.com/vicalloy/telegram-shell-bot</a></p>


<p>很早之前就打算做这么一个东西，后来因为要调用的脚本是 Python 写的，于是直接在 Bot 里调用 Python 代码。 近期把这个想法重新实现了。</p>


<p>网上也有类似的项目，其中一些比较简单，甚至都没有对用户做校验，这样只要有人可以连上你的机器人就可以控制你的机器。类似的机器人里功能最强的是<a href="https://github.com/botgram/shell-bot">shell-bot</a>。&#8221;shell-bot&#8221;模拟了一个 tty，实现较复杂，没有仔细研究。</p>


<p><a href="https://github.com/vicalloy/telegram-shell-bot">Telegram Shell Bot</a>目前提供的功能有：</p>


<ol class="wp-block-list"><li>鉴权，只有在许可列表里的用户才能对机器人发号指令。</li><li>支持命令的黑白名单。注：为避免使用 <code>;</code> 跳过命令检查，类似的字符也应当加到字符串黑名单中。</li><li>Shell 命令的执行，如 ls、cat、ps 等。</li><li>长时间执行命令的管理。如执行 wget 操作，只返回最开始几条输出（防止一直刷屏）。之后可以通过<code>/tasks</code>命令查看有哪些命令还在执行中，并可以通过 /kill pid 的方式强制结束命令。</li><li>支持 sudo。注：sudo 通过 <code>echo password | su -S</code> 的方式实现，需要自行评估风险。</li><li>自定义脚本放在 ./scripts 目录，通过 /script 命令可以快速访问这些脚本并执行。</li></ol>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Telegram机器人</title>
		<link>https://haoluobo.com/2019/02/telegram-bot/</link>
		
		<dc:creator><![CDATA[vicalloy]]></dc:creator>
		<pubDate>Sat, 23 Feb 2019 05:23:08 +0000</pubDate>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[telegram]]></category>
		<guid isPermaLink="false">/?p=11730</guid>

					<description><![CDATA[最近重新开始玩Ingress。好多年没玩，主要玩家已由QQ转战Telegram了。还有玩家专门为Telegra [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>最近重新开始玩<a href="https://www.ingress.com/">Ingress</a>。好多年没玩，主要玩家已由QQ转战Telegram了。还有玩家专门为Telegram做了个Bot用来做新人接待、面基统计等相关工作。稍微研究了一下Telegram的Bot实现，发现Telegram API功能非常强大，而且使用起来也很简单，可以轻易的做出自己的机器人。</p>



<p>如果你想更多的了解Telegram Bot可以做什么，怎么创建一个自己的Bot建议阅读Telegram的官方文档 <a href="https://core.telegram.org/bots">Bots: An introduction for developers</a>。如果你和我一样使用Python进行开发，可以使用<a href="https://github.com/python-telegram-bot/">python-telegram-bot</a>进行开发。</p>



<h2 class="wp-block-heading">让Telegram Bot主动推送消息</h2>



<p>一般情况下Bot都是在接收到用户的命令后被动的回复信息，如果希望机器人主动推送消息可以先手动查询<code>chat id</code>，然后Bot发送消息时指定为该<code>chat id</code>。获取<code>chat id</code>的方法如下：</p>



<ol class="wp-block-list"><li>和机器人对话。如果希望获取group的id，这需要先将机器人加到group，再<code>@bot /xxx</code>给机器人发消息。</li><li>访问&nbsp;<code>https://api.telegram.org/bot&lt;YourBOTToken&gt;/getUpdates</code>获取消息。</li><li>访问<code>getUpdates</code>接口后将得到一组JSON数据，里面哪个是<code>chat id</code>还是比较容易识别出来的。</li></ol>



<h2 class="wp-block-heading">一个简单的机器人实例</h2>



<p>发送命令<code>51job</code>，这个机器人会调用<code>jobmonitor</code>检查51job的岗位更新情况。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
import logging
from telegram.ext import Updater, CommandHandler
from telegram.ext.dispatcher import run_async
from job import qcwy as job_qcwy
TOKEN = &#039;XXX&#039;
# Enable logging
logging.basicConfig(format=&#039;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#039;,
                    level=logging.INFO)
logger = logging.getLogger(__name__)
@run_async
def start(bot, update):
    &quot;&quot;&quot;Send a message when the command /help is issued.&quot;&quot;&quot;
    bot.send_message(
        chat_id=update.message.chat_id,
        text=&quot;Hi! I&#039;m vicalloy&#039;s Bot. \r\n&quot;
        &quot;/51job update 51job. n&quot;
    )
@run_async
def qcwy(bot, update):
    # 为防止其他人恶意向机器人发送信息触发该命令，这里还需要对发送人做个判断，只响应特定用户的请求。
    job_qcwy()
def error(bot, update):
    &quot;&quot;&quot;Log Errors caused by Updates.&quot;&quot;&quot;
    logger.warning(&#039;Update &quot;%s&quot; caused error &quot;%s&quot;&#039;, bot, update.error)
def main():
    updater = Updater(TOKEN)
    dp = updater.dispatcher
    dp.add_handler(CommandHandler(&quot;start&quot;, start))
    dp.add_handler(CommandHandler(&quot;help&quot;, start))
    dp.add_handler(CommandHandler(&quot;51job&quot;, qcwy))
    dp.add_error_handler(error)
    updater.start_polling()
    updater.idle()
if __name__ == &#039;__main__&#039;:
    main()
</pre></div>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
