NUS Hackers Wiki
NUS Hackers Wiki
  • NUS Hackers Wiki
  • Hackerschool
    • Virtual Machines and Linux
    • Beginners' Guide to the Terminal
      • Introduction to the Terminal
      • Modern Shell Tools
      • Shell Scripting
      • Real World Scripting
      • Resources
    • Self-Hosting: Three Easy Pieces
      • 1. Setting up your server
      • 2. Running Services
      • 3. Monitoring your server
    • Vim
    • Introduction to Zig
      • Language Basics
      • Error Handling
      • Memory Management
      • Working with C
      • Exploring comptime
    • CI/CD with Github Actions
      • Background
      • Basics of Github Actions
        • Target workflow
        • Running unit tests
        • Linting code
        • Deploying to Github Pages
      • Advanced use cases
        • Pollers
        • Github script
        • Executing third-party scripts
        • Reusable workflows
      • Cookbook
    • Lightning Git
      • Git Concepts
      • Getting Started with Git
      • Making your first commit
      • Branching
      • Merge Conflicts
      • Integrating remote repositories
      • Collaborative Workflows
      • Commit Manipulation and Reflog
      • Interactive rebasing
      • filter-repo
  • Orbital
    • JavaScript
      • Browser Developer Tools
      • Getting Started
      • Datatypes
      • Operators and Operations
      • Loops and Conditions
      • Functions
      • Strings
      • Arrays
      • HTML
        • Getting Started
        • Tag Attributes
        • HTML Forms
        • Browser Inspector
      • CSS
        • Selectors
        • Colors in CSS
        • Measurements in CSS
        • The Box Model
        • Adding Styles - Part 1
        • Adding Styles - Part 2
      • Working with the DOM
        • Querying the DOM - Selectors
        • Querying the DOM - Element Attributes
        • Querying the DOM - Element Styles
        • Events with JS and HTML
        • Exercise: Click Counter
        • Editing the DOM
        • Fetch Requests
        • Exercise: The NUSMods API
    • React
      • Setup
      • State
    • React Native
      • Setup
      • Intro to JSX
      • Basic Syntax
      • Handling UI
      • Props
      • State Management
    • Git
      • Setup
      • Command Glossary
      • Fundamental Concepts
        • Getting Started
        • Integrating Remote Repositories
        • Branching
        • Merge Conflicts
      • Collaborative Workflows
        • Fork and PR Workflow
        • Branch and PR Workflow
      • Advanced Concepts
        • Ignoring Files
        • Commit Message Conventions
        • Github Collaborators
        • CI/CD with Github Actions
        • Advanced Git Commands
      • FAQ
    • Telegram Bot
      • Creating a TeleBot
      • API Calls
      • Telebot Basics
      • Integrating API's
    • Relational Database
      • Database Overview
      • Database Design
      • Entity Relationship Diagram
      • SQL Basics & PostgreSQL
    • TypeScript
      • Types and Interfaces
      • Utility Types
      • Typing Component Props, Events, and Hooks
      • Why You Should Avoid Using any (and What to Do Instead)
      • TypeScript Tricks You’ll Use All the Time in React
Powered by GitBook
On this page
  • Getting Started
  • Creating a Bot
  • Import Packages
Edit on GitHub
Export as PDF
  1. Orbital
  2. Telegram Bot

Creating a TeleBot

PreviousTelegram BotNextAPI Calls

Last updated 12 months ago

Getting Started

  1. Open up the IDE of your choice (PyCharm, VS Code, Google Colab, etc.)

  2. Locate the terminal - usually found in a panel at the bottom or side of your IDE

  3. Install the telebot package by typing pip install python-telegram-bot Note: Colab uses ! as a prefix to shell commands

  4. Run the command

Link to the Library:

Creating a Bot

  • Launch Telegram

  • Search for the user @BotFather

  • Create a new bot by using the command '/start '

  • Follow the prompts and set the bot's name and username Username must end with "bot" -> e.g. 'untitled_bot'

  • Receive your bot token: BotFather will provide you with a unique token that will be used to authenticate your bot

Keep your token secure and do not share it publicly

  • Input token into your environment

# Replace "YOUR_API_TOKEN" with token received from BotFather

token = "YOUR_API_TOKEN"
  • (Optional) Add a description, profile picture, etc.

Import Packages

We will be creating the telebot in the context of Google Colab!

So, let's get started! Paste the following code blocks into your file:

  1. Allow for asynchronous programming capabilities

# Workaround for nested event loops in Jupyter (Google Colab)
# Jupyter notebooks already have a running loop via Tornado and the asyncio lib does not allow a nested loop, we need to use a separate package called nest_asyncio

import asyncio
import nest_asyncio

nest_asyncio.apply() 

# for non-Jupyter notebook IDE's

import asyncio
  1. Enable logging

import logging

logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', 
    level=logging.INFO
)

logger = logging.getLogger(__name__)

This is setting up logging module, so you know when (and why) things don't work as expected.

  1. Import from telegram package

from telegram import Update
from telegram.constants import ParseMode
from telegram import InlineQueryResultArticle, InputTextMessageContent
from telegram.ext import filters, MessageHandler, ApplicationBuilder, CommandHandler, ContextTypes, InlineQueryHandler
  1. (Optional) Other useful packages

import math
from random import random
import datetime

Click for more info.

here
https://github.com/python-telegram-bot/python-telegram-bot
BotFather on Telegram
Your Bot's Token