-1

I want to code and creat a bot for telegram that does these things:

1 - shows a massage to the person that hit start button

2 - then it gets a name as an input

3 - then again shows a massage

4 - getting an input

5 - at the end add the inputs to a defualt text and showing it;

for exampele:

-start

+Hi What is your name?

-X

+How old are you?

-Y

+Your name is X and you are Y years old.

My second Question is that how can I Connect to bots together, for example imagine I want to pass some input from this bot to make a poll(voting massage), in order to do that I should send the name to let's say @vote, how is that possible and what should I learn to do such things with my bot?

CallMeStag
  • 5,467
  • 1
  • 7
  • 22

1 Answers1

-1

First you're gonna have to explore telegram bot API documentation here.

Then you should choose your programming language and the library you want to use.

There are different libraries for each language, I'm gonna name a few:

I'm gonna give you an example for what you want in python using pyTelegramBotAPI:

First install the library using pip:

pip install git+https://github.com/eternnoir/pyTelegramBotAPI.git

Then run this script:

import telebot

API_TOKEN = 'PLACE_BOT_TOKEN_HERE'

bot = telebot.TeleBot(API_TOKEN)

user_info = {}


def set_user_state(user_id, state):
    if user_id not in user_info:
        user_info[user_id] = {}

    user_info[user_id]["state"] = state


def get_user_state(user_id):
    if user_id in user_info:
        if "state" in user_info[user_id]:
            return user_info[user_id]["state"]
    return "Welcome"


def set_user_info(user_id, name=None, age=None):
    if name is None and age is None:
        return
    if name is not None:
        user_info[user_id]["name"] = name
    if age is not None:
        user_info[user_id]["age"] = age


def get_user_info(user_id):
    return user_info[user_id]


@bot.message_handler()
def echo_all(message):
    user_id = message.from_user.id

    if message.text == "/start":
        bot.reply_to(message, "Hi What is your name?")
        set_user_state(user_id, "EnterName")
        return

    user_state = get_user_state(user_id)

    if user_state == "EnterName":
        set_user_info(user_id, name=message.text)
        bot.reply_to(message, "How old are you?")
        set_user_state(user_id, "EnterAge")
        return
    if user_state == "EnterAge":
        set_user_info(user_id, age=message.text)
        info = get_user_info(user_id)
        bot.reply_to(message, "Your name is %s and you are %s years old." %(info["name"], info["age"]))
        set_user_state(user_id, "Welcome")
        return
    bot.reply_to(message, "To restart please send /start")


bot.infinity_polling()

Here we use a dictionary to place user state and info, you can place them anywhere like databases or json files.

Then we update a user's state based on their interactions with the bot.


For your second question, bots cannot communicate with each other so you should look for other solutions. In the case of your question where you want to create a poll, you should check sendPoll method as well as PollAnswer object which you receive when a user votes in a poll.

Ali Padida
  • 1,763
  • 1
  • 16
  • 34