Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I upload my telegram bot's code to run?

I know I create new bot, give it name, description from BotFather inside telegram

But this only adds the bot, when I modify my bot, code some functionality in python\lua\php etc - where should the code go and how telegram will know the behavior of my bot?

Who runs the new code, where should I upload my new additional code for my bot?

Does it go to telegram server and runs there on the cloud? If so, how to upload it?

like image 865
ERJAN Avatar asked Feb 24 '17 04:02

ERJAN


2 Answers

After you have setup your Bot's identity (@bot_name) with BotFather, the next step is to design the interaction/functions your Bot will perform.

Your bot code lives on YOUR server.

Requests from users interacting with your @bot_name will be routed from Telegram to your servers which ...

1) you have setup with a webHook (using the setWebhook method) so Telegram knows where to send your bot's requests

or

2) your bot polls Telegram's Bot-API repeatedly asking if there are any new updates (i.e. messages users sent to your bot) using the getUpdates method

Your bot receives these messages, and replies as directed by your bots "code or logic"

hope this helps.

like image 66
Charles Okwuagwu Avatar answered Nov 10 '22 19:11

Charles Okwuagwu


You can run the code quite easily from your machine.

For example how I did it using NodeJS:

1)Install NodeJS on your machine (details - https://nodejs.org/en/download/package-manager/)

2)Install Node Telegram Bot API (https://github.com/yagop/node-telegram-bot-api)

3) create a file like this, filling in with necessary changes:

const TelegramBot = require('node-telegram-bot-api');

// replace the value below with the Telegram token you receive from @BotFather
const token = 'YOUR_TELEGRAM_BOT_TOKEN';

// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});

// Matches "/echo [whatever]"
bot.onText(/\/echo (.+)/, (msg, match) => {
  // 'msg' is the received Message from Telegram
  // 'match' is the result of executing the regexp above on the text content
  // of the message

  const chatId = msg.chat.id;
  const resp = match[1]; // the captured "whatever"

  // send back the matched "whatever" to the chat
  bot.sendMessage(chatId, resp);
});

// Listen for any kind of message. There are different kinds of
// messages.
bot.on('message', (msg) => {
  const chatId = msg.chat.id;

  // send a message to the chat acknowledging receipt of their message
  bot.sendMessage(chatId, 'Received your message');
});

4) Finally launch your command console (like cmd on Windows) navigate to telegram bot directory where the script is located and type node index.js (assuming your file with the bot script like above is named index.js)

Following these steps you will have a fully functioning bot. As you make changes to index.js you can simply rerun the command "node index.js" in console.

The procedure is similar if you need to set up a bot on a server.

Hope this helps.

like image 2
Kristian Vybiral Avatar answered Nov 10 '22 19:11

Kristian Vybiral