name: How to create a Telegram Bot class: center, middle, inverse
- What is a bot?
- The Botfather
- API
- Python Telegram Bot Project
- Write your own bot
Bots are simply Telegram accounts operated by software – not people – and they'll often have AI features.
They can do anything – teach, play, search, broadcast, remind, connect, integrate with other services, or even pass commands to the Internet of Things.
-- Telegram blog
-
No online status and no last seen timestamps. --
-
Limited cloud storage. --
-
Can't initiate conversations with users. Users must start conversation or add to groups. The link is
telegram.me/<bot_username>
. -- -
Usernames always end in 'bot'. --
-
When added to a group, bots do not receive all messages by default. --
-
Bots never eat, sleep or complain (unless expressly programmed otherwise).
class: center
-- The Lord of the Bots, J.R.R.Tolkien
-
/newbot
-- -
Enter name of the Bot; e.g. Isfahan LUG --
-
Enter the username of the bot; e.g. ilugbot. (It should end in 'bot') --
-
Write down the token.
-
/setcommands
-- -
send All of the commands in the following format:
command1 - Description
command2 - Another description
The description is a text, up to 512 characters long, which describes your bot.
It will be shown at first of every conversation with the bot.
/setdescription
- Send the description of the bot to botfather --
/setabouttext
- Send the about text of the bot to botfather --
.right[.bottom[check out other commands with /help
]]
- The Bot API is an HTTP-based api. It supports GET and POST requests. --
-
All queries must be served over HTTPS and in this form:
https://api.telegram.org/bot<token>/METHOD_NAME
-- -
All queries must be made using UTF-8. --
-
All methods are case-insensitive. --
-
The response is a JSON object which:
- always has an 'ok' boolean field. --
- has a 'result' field if the response is ok. --
- may have an optional 'description' field which contains some info about the occurred error. --
- also has a 'error_code' in the case of errors.
- Use Postman in order to test the API.
- Use Postman in order to test the API.
In order to write out bot, we need an interface. Here we use python-telegram-bot
which is written in Python.
It is under development and is well written. Good documentation and has wiki.
check out the project site.
- Installing
pip install --upgrade python-telegram-bot
* Install [python pip](https://pip.pypa.io/en/latest/installing.html) first.
--
- Using
>>> import telegram
>>> bot = telegram.Bot(token='your_bot_token')
- To get Bot's info:
>>> print bot.getMe()
- To get updates:
>>> updates = bot.getUpdates()
>>> print [u.message.text for u in updates]
- To send a text message:
>>> chat_id = bot.getUpdates()[-1].message.chat_id
>>> bot.sendMessage(chat_id=chat_id, text="Can I help you?")
- To send an Emoji:
>>> bot.sendMessage(chat_id=chat_id, text=telegram.Emoji.SEE_NO_EVIL_MONKEY)
class: center, middle
class: center, middle