Send message from Pico LTE to Telegram

Send a message from the Pico LTE to a Telegram chat through a Telegram bot, over the cellular connection.

This example sends a message from the Pico LTE to a Telegram chat through a Telegram bot and the Telegram Bot API, over the cellular connection. No extra hardware is needed.

Before you begin

Required

Hardware

  • Sixfab Pico LTE or Pico LTE 2Registered and active in Sixfab Connect, connected to the computer with the micro USB cable.

Software

Create bot

  1. Start BotFather.

    Search for BotFather in Telegram and start a chat with it. /start lists its commands.

  2. Create bot.

    Send /newbot, then the bot’s name, then its username. The name and the username are different: the next steps use the username.

  3. Keep token.

    BotFather gives you a token for the HTTP API, such as 1234567890:XXX9WhkY694zP856M4xNLcr9pXX-xxxxxxx.

    Keep the token secret: anyone with it controls the bot.

Find chat ID

  1. Start chat with bot.

    Open the bot in Telegram and send /start.

  2. Read chat ID.

    Open https://api.telegram.org/bot<YOUR_ACCESS_TOKEN>/getUpdates in a browser, with your token. The chat ID is the id of chat:

    {
      "ok": true,
      "result": [
        {
          "update_id": 679821252,
          "message": {
            "message_id": 15,
            "from": {
              "id": 70*******,
              "is_bot": false,
              "first_name": "YOUR_NAME",
              "username": "YOUR_USERNAME",
              "language_code": "en"
            },
            "chat": {
              "id": 70*******,    ⬅ CHAT_ID
              "first_name": "*",
              "username": "*",
              "type": "private"
            },
            "date": 1686954941,
            "text": "/start",
            "entities": [
              {
                "offset": 0,
                "length": 6,
                "type": "bot_command"
              }
            ]
          }
        }
      ]
    }

    If the result is empty, see Troubleshooting.

Run example

  1. Set token and chat ID in config.json.

    {
        "telegram": {
            "token": "YOUR_BOT_TOKEN_ID",
            "chat_id": "YOUR_GROUP_CHAT_ID"
        }
    }
  2. Run script.

    Open examples/telegram/send_message.py from the SDK repository in Thonny and click Run.

    INFO: Sending message to Telegram channel...
    INFO: Result: {'status': 0, 'response': ['+QHTTPGET: 0,200,268', '{"ok":true,"result":{"message_id":14,"from":{"id":5612730189,"is_bot":true,"first_name":"Pico LTE","username":"Pico LTE BOT"},"chat":{"id":70*******,"first_name":"YOUR_NAME","username":"YOUR_USERNAME","type":"private"},"date":1696047950,"text":"PicoLTE Telegram Example'], 'interval': 0}
  3. Check chat.

    The message appears in the chat with the bot.

Troubleshooting

If /getUpdates returns {"ok":true,"result":[]} and no chat ID, send a few messages to the bot, then reload the page.