擅长:python、mysql、java
<p>第一步是创建一个<a href="https://core.telegram.org/bots#6-botfather" rel="nofollow noreferrer">bot</a>并获取<code>token</code>。</p>
<p>第二步是获取<code>chat_id</code>:</p>
<ul>
<li>在聊天中写点东西</li>
<li>访问<code>https://api.telegram.org/bot<YourBOTToken>/getUpdates</code>,在<code>message['chat']['id']</code>键下获取<code>chat_id</code>。</li>
</ul>
<p>最后一步是使用以下代码:</p>
<pre class="lang-py prettyprint-override"><code>import requests
def telegram_bot_sendtext(bot_message):
bot_token = ''
bot_chatID = ''
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
test = telegram_bot_sendtext("Testing Telegram bot")
print(test)
</code></pre>
<p>从<a href="https://medium.com/@ManHay_Hong/how-to-create-a-telegram-bot-and-send-messages-with-python-4cf314d9fa3e" rel="nofollow noreferrer">Medium.com: How to create a Telegram bot, and send messages with Python</a>提取的代码</p>