Telegram¶
Integrate your code with Telegram to send messages, files, and more.
Installation¶
Setting up the API Token¶
We need to create a Telegram bot
and generate a token for it so that your code can interact with the Telegram API.
Creating BotTelegram:¶
-
Open a chat with
@BotFather
on your Telegram. It will be on your users' list. -
Send the command
/start
. -
Send the command
/newbot
. -
The BotFather will ask for a name and a username for your bot.
Your bot is ready, and your token will appear in the conversation with @BotFather
.
Start Bot¶
Now, it will be necessary to start your bot by clicking on the link that @BotFather
makes available next to your token. It will direct you to a conversation with your bot, so just click on the start
button.
Group privacy settings¶
Using the chat with @BotFather
, it will be necessary to turn off the group's Privacy settings using the following:
- Send the command
/mybots
. - Choose the bot you just created.
- Click on
bot settings
. - Click on
Group Privacy
. - Click on
turn off
the group privacy.
Adding a bot to a group¶
With your Telegram bot
created and your token in hand, add your bot to the group you want to interact with and start using the plugin.
Important
We suggest that after starting your first bot and including it in a group, you send a manual message to the bot so that the updates identify the created group. The Telegram API has a bug where, in some cases, the updates only recognize the group from a manually initiated message. If you receive the error Bad Request: chat not found when trying to send a message, remember to start a manual interaction with the group to which it was added.
Importing the Plugin¶
After you install this package, the next step is to import the package into your code and start using the functions.
Send Message¶
from botcity.plugins.telegram import BotTelegramPlugin
# Instantiate the plugin
telegram = BotTelegramPlugin(token='<your_token_bot_telegram>')
# Send Message
response = telegram.send_message(
text="This is an automated test message through my method.",
group="BotGroupTest",
username=["@TestBotCitybot"]
)
Here is the expected output:
Editing a Message¶
from botcity.plugins.telegram import BotTelegramPlugin
# Instantiate the plugin
telegram = BotTelegramPlugin(token='<your_token_bot_telegram>')
# Send Message
response = telegram.send_message(
text="This is an automated test message through my method.",
group="BotGroupTest"
)
# Edit Message
telegram.edit_message(
text="This is an edited message",
response=response,
username=["@TestBotCitybot"]
)
Here is the expected output:
Uploading a Document¶
Here is the expected output:
Deleting a Message/File¶
from botcity.plugins.telegram import BotTelegramPlugin
# Instantiate the plugin
telegram = BotTelegramPlugin(token='<your_token_bot_telegram>')
# Send Message
response = telegram.send_message(
text="This is an automated test message through my method.",
group="BotGroupTest"
)
# Delete Message
delete = telegram.delete_message(response=response)
# Upload Document
response = telegram.upload_document(
document='test_document.zip',
group="BotGroupTest",
caption="This is a document upload"
)
# Delete upload
delete = telegram.delete_message(response=response)