Telegram Bot Development: Advanced

Sunil Hirole
GAMMASTACK
Published in
4 min readNov 4, 2019
Developing a cool new telegram bot

In the last tutorial, we went through the basics of developing a telegram application. Now we will be covering a few of the advanced features:

  1. Image Sharing
  2. Keyboard Functionality
  3. Location Sharing

Image Sharing

In the previous tutorial, we covered how to send text messages and responses to the commands using bot. Now in order to enable the functionality of sharing an image by the bot, we will be using the sendPhoto function of telegram bot instance which uses the telegram bot API.

bot.onText(/\/sendlogo/, msg => {
bot.sendPhoto(msg.chat.id, 'https://www.gammastack.com/wp- content/uploads/2019/07/cropped-logo-formely-new-1.png', {caption: "This is Gammastack's logo"})
})

Here we have created a command /sendLogo to which bot will respond with an image. In this case, it is Gammastack’s logo.
You can see with the sendPhoto function we passed an argument called caption, which will be used, as the name suggests, to add a caption to the shared image.

Keyboard Functionality

Now suppose you have an application wherein lot of messages are being circulated. You would not want to force your users to write all the messages every time and send it to the bot. Instead, we can show them some alternatives that they can tap on and send a specific message to the bot. Won’t that be nice!!!

The keyboard is an interactive way of sending messages.

In order to introduce the keyboard functionality in our application, we’ll be using the sendMessage function. We can achieve this by passing the below to the sendMessage function as a parameter:

bot.onText(/\/start/, msg => {
bot.sendMessage(
msg.chat.id,
`Hi, ${msg.chat.first_name} ${msg.chat.last_name}.
Welcome to Gammastack's contact telegram bot.
Select 'HR' to get HR's contact number
Select 'Sales' to get sales team contact number
Select 'SalesMail' to get sales team email id
Select 'Mail' to get company's email id
Select 'Logo' for Gammastack's logo
Select 'Location' for Gammastack's location`,
{
reply_markup: {
keyboard: [['HR', 'Sales'], ['SalesMail', 'Mail'], ['Logo', 'Location']]
}
}
)
})
  • To test the start command type /start in the text input field and press enter and it will show you the response like this.
/start command response
  • Let’s create the message HR and its response:
const hr = 'HR'
if (msg.text === hr) {
bot.sendMessage(msg.chat.id, 'The contact number of HR is
+917312974574')
}
  • Now you can click on HR button and you should get The contact number of HR is +91732974574 as response.

Location Sharing

Now we’ll explore how a location can be shared using bot. We’ll be using sendLocation function of telegram bot instance which uses the telegram bot API.

The location has to provided in the form of coordinates and a message can also be shared together with those coordinates. For the below demonstration we are creating a message Location and the response will be a map. This is how we do it.

const location = 'Location'   
if (msg.text.indexOf(location) === 0) {
bot.sendLocation(msg.chat.id, 22.685664, 75.872153)
bot.sendMessage(msg.chat.id, 'Here is the location of Gammastack') }

So we’ve learned how to create a bot application in telegram from scratch. Now you can create a bot application and can modify it according to your requirement. Here is a gist with a telegram contact bot.

Contact Us

About Gammastack

There are several recognized software companies heading their way into Telegram Bot Development, one among them is GammaStack. The Telegram Bots offered by us comes with a myriad of unparalleled features.

Along with Telegram Open Network Blockchain Network Development, we are also known for offering top of the line iGaming software including sports betting software and fantasy sports software. Whether you are a start-up or an enterprise-level business, we can be your perfect helping hands for Telegram Bot Development solutions. Possessing a team of skilled and experienced developers, we are capable of delivering the exact solutions as per your requirement.

If you are looking for Telegram Bot and Telegram Open Network Blockchain Network Development, then GammaStack is one stop solution for you. The bots we develop have features such as interactive applications using the keyboard, in-app location sharing, crypto and fiat payment support, etc. Get more details at:

https://www.gammastack.com/telegram-bot-and-telegram-open-network-blockchain-network-development/

--

--

Sunil Hirole
GAMMASTACK

Senior Solution Engineer at Gammastack, Node.js | Angular | React | Solidity | Hyperledger Fabric | Crypto Exchanges/Wallets | Docker