Wongsakorn Sanwises
2 min readApr 16, 2022

--

เขียน Bot บน Telegram ด้วย Python

สร้าง token ด้วยการแอด @BotFather

พิมพ์คำสั่ง /start เพื่อเริ่ม

พิมพ์คำสั่ง /newbot เพื่อสร้างบอท

ตั้งชื่อชื่อบอท ให้ลงท้ายชื่อด้วยทำว่า bot

ไปลองดู Doc API https://telegram-bot-sdk.readme.io/reference/getme

สำหรับ Chat ID ให้และวิธีหา

ลองส่งข้อความ https://telegram-bot-sdk.readme.io/reference/sendmessage

import requeststoken = " Token ที่ได้จากการสร้าง "
Chat_id = " Chat ID ใส่ที่นี่ "
def send_msg(text):url = f”https://api.telegram.org/bot{token}/sendMessage"
payload = {
“text”: text,
“disable_web_page_preview”: False,
“disable_notification”: False,
“reply_to_message_id”: None,
“chat_id”: Chat_id
}
headers = {
“Accept”: “application/json”,
“Content-Type”: “application/json”
}
response = requests.request(“POST”, url, json=payload, headers=headers)print(response.text)send_msg(‘Hello!! from Python’)

--

--