Telegram Bot 跟我想的不一樣. 第一天進公司時,Slack就被邀進同事們訂便當的群組,在上面會宣布今天訂那一家便… – Telegram Group

Telegram

Telegram Bot 跟我想的不一樣

  • 到LINE DEV 申請一個新的Channel,並紀錄channel secret
  • 安裝 line-bot-sdk-python
  • 使用Python API 框架(現在我自己比較喜歡FastAPI,但也使用過Django、Flask)
  • 本機測試需安裝ngrok,產生對應API port的HTTPS網址
  • 在LINE DEV Channel 設定ngrok產生的網址於webhook
  • 接下來就可以開始測試了
  • 申請Bot Token(等同LINE DEV 申請一個Channel
  1. 到Telegram搜尋 @BotFather 並加入
  • 建置API server(接收webhook 及發訊息
  1. 我自己是使用Python FastAPI建置,任何API 框架皆可
from fastapi import FastAPI

app = FastAPI()

@app.post("/bot/callback")
async def say_hello(request: Request):
client_host = request.client.host
print(await request.body())
return {"client_host": client_host}
./ngrok http 8000
https://api.telegram.org/bot{my_bot_token}/setWebhook?url={url_to_send_updates_to}
https://api.telegram.org/bot{my_bot_token}/getWebhookInfo
  • 設定好後就將API啟動,並在Telegram中說話,然後看API console
{"update_id": XXXXXXXXXX,\n"message": {"message_id": 17,"from": {"id": XXXXXXXXXX,"is_bot": false,"first_name": "Morris","last_name": "Lin","username": "XXXXXXXXXX","language_code": "en"},"chat": {"id": XXXXXXXXX,"first_name": "Morris","last_name": "Lin","username": "XXXXXXXXX","type": "private"},"date": 1642155727,"text": "hihi bot webhook test"}

python-telegram-bot

def main() -> None:
"""Start the bot."""
# Create the Updater and pass it your bot's token.
updater = Updater("你的Token")

# Get the dispatcher to register handlers
dispatcher = updater.dispatcher

# on different commands - answer in Telegram
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help_command))

# on non command i.e message - echo the message on Telegram
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))

# Start the Bot
updater.start_polling()

# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()


if __name__ == '__main__':
main()

Ten articles before and after

How to Create a Telegram Chatbot in 2022 – Telegram Group

Google Sheets Formula & Telegram Message ✈️ – Telegram Group

Monitor Server with Telegram Bot and Python – Telegram Group

How to create a Telegram bot with Python in under 10 min! – Telegram Group

How to Install and Run the Crypto-Telegram Bot – Telegram Group

TRX1 Dev Blog #11 (November 2021) – Telegram Group

Serverless Telegram bot with Kotlin, Firebase and Google Cloud Functions – Telegram Group

How To: Deploy Java Telegram Bot to Heroku – Telegram Group

How To: Create a Java Telegram Bot – Telegram Group

How I Use A Telegram Bot To Track My Expenses – Telegram Group

About Me

Pretium lorem primis senectus habitasse lectus donec ultricies tortor adipiscing fusce morbi volutpat pellentesque consectetur risus curae malesuada dignissim lacus convallis massa mauris.