Last active
January 16, 2024 08:07
-
-
Save idriszmy/f6e7587880247dcd5cc4a7fff423d04e to your computer and use it in GitHub Desktop.
Telegram Bot using CircuitPython on Raspberry Pi Pico W
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Telegram Bot using CircuitPython on Raspberry Pi Pico W | |
Items: | |
- Maker Pi Pico Mini | |
https://my.cytron.io/p-maker-pi-pico-mini-simplifying-projects-with-raspberry-pi-pico | |
- USB Micro B Cable | |
https://my.cytron.io/p-usb-micro-b-cable | |
CircuitPython Raspberry Pi Pico W | |
https://circuitpython.org/board/raspberry_pi_pico_w/ | |
- Tested with CircuitPython 8.0.0-beta.2 | |
CircuitPython Additional libraries | |
https://circuitpython.org/libraries | |
- adafruit_requests.mpy | |
- simpleio.mpy | |
""" | |
import time | |
import microcontroller | |
import board | |
import digitalio | |
import simpleio | |
import wifi | |
import socketpool | |
import adafruit_requests | |
import ssl | |
# Get wifi details and more from a secrets.py file | |
try: | |
from secrets import secrets | |
except ImportError: | |
print("All secret keys are kept in secrets.py, please add them there!") | |
raise | |
# Telegram API url. | |
API_URL = "https://api.telegram.org/bot" + secrets["telegram_bot_token"] | |
NOTE_G4 = 392 | |
NOTE_C5 = 523 | |
buzzer = board.GP18 | |
led = digitalio.DigitalInOut(board.LED) | |
led.direction = digitalio.Direction.OUTPUT | |
def init_bot(): | |
get_url = API_URL | |
get_url += "/getMe" | |
r = requests.get(get_url) | |
return r.json()['ok'] | |
first_read = True | |
update_id = 0 | |
def read_message(): | |
global first_read | |
global update_id | |
get_url = API_URL | |
get_url += "/getUpdates?limit=1&allowed_updates=[\"message\",\"callback_query\"]" | |
if first_read == False: | |
get_url += "&offset={}".format(update_id) | |
r = requests.get(get_url) | |
#print(r.json()) | |
try: | |
update_id = r.json()['result'][0]['update_id'] | |
message = r.json()['result'][0]['message']['text'] | |
chat_id = r.json()['result'][0]['message']['chat']['id'] | |
#print("Update ID: {}".format(update_id)) | |
print("Chat ID: {}\tMessage: {}".format(chat_id, message)) | |
first_read = False | |
update_id += 1 | |
simpleio.tone(buzzer, NOTE_G4, duration=0.1) | |
simpleio.tone(buzzer, NOTE_C5, duration=0.1) | |
return chat_id, message | |
except (IndexError) as e: | |
#print("No new message") | |
return False, False | |
def send_message(chat_id, message): | |
get_url = API_URL | |
get_url += "/sendMessage?chat_id={}&text={}".format(chat_id, message) | |
r = requests.get(get_url) | |
#print(r.json()) | |
print(f"Initializing...") | |
while not wifi.radio.ipv4_address or "0.0.0.0" in repr(wifi.radio.ipv4_address): | |
print(f"Connecting to WiFi...") | |
wifi.radio.connect(ssid=secrets["ssid"], password=secrets["password"]) | |
print("IP Address: {}".format(wifi.radio.ipv4_address)) | |
pool = socketpool.SocketPool(wifi.radio) | |
requests = adafruit_requests.Session(pool, ssl.create_default_context()) | |
if init_bot() == False: | |
print("Telegram bot failed.") | |
else: | |
print("Telegram bot ready!\n") | |
simpleio.tone(buzzer, NOTE_C5, duration=0.1) | |
while True: | |
try: | |
while not wifi.radio.ipv4_address or "0.0.0.0" in repr(wifi.radio.ipv4_address): | |
print(f"Reconnecting to WiFi...") | |
wifi.radio.connect(ssid=secrets["ssid"], password=secrets["password"]) | |
chat_id, message_in = read_message() | |
if message_in == "/start": | |
send_message(chat_id, "Welcome to Telegram Bot!") | |
elif message_in == "LED ON": | |
led.value = True | |
send_message(chat_id, "LED turn on.") | |
elif message_in == "LED OFF": | |
led.value = False | |
send_message(chat_id, "LED turn off.") | |
else: | |
send_message(chat_id, "Command is not available.") | |
except OSError as e: | |
print("Failed!\n", e) | |
microcontroller.reset() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Secret Keys. | |
secrets = { | |
"ssid" : "my_wifi_ssid", | |
"password" : "my_wifi_password", | |
"telegram_bot_token" : "my_telegram_bot_token", | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment