We want to build a cutting-edge telegram bot that has access to web3, so let's begin by creating our virtual environment

python -m venv venv

Activate it on windows

.\venv\Scripts\activate

On Linux

source venv/bin/activate

Install the required modules

pip install python-dotenv python-telegram-bot web3 requests

Create the main_bot.py file image

now create a bot with t.me/botfather

image

create a .env file and add your bot token

TG_BOT_TOKEN = "6660539960:AAEKhbQoYG70409jvKAE8tAjv3tEEdq8JCo"

now you can run the bot with

python .\main_bot.py

image

Let's try it out!

image

We created our bot and it's on! yay!

Let's add the web3 part.

Create a folder called /core
then create another folder called /web3
Then create a file called web3_main.py

image

Let's define some basic web3 calls to the chain image

Now let's back to the main and create a function for our new command

image

Little trick with python now

image

I'm not quite sure the object update.message contains this... how can i know that?
2 ways

  • Go to the documentation
  • add Types to variables We'll pick n°2 just because it's a nice thing to do. Import ContextTypes & Update
from telegram.ext import (CommandHandler, Application, ContextTypes)

from telegram import Update

Add those to the function parameters
Hover the function again and...

image

🥳🥳🥳 it was right!

Now let's add the command image And import our new module

# local modules

from core.web3.main_web3 import Web3

Run the bot again and send the /chain command image

🥳