bot 被其他 getUpdates 请求终止确保只有一个 bot 实例是 运行

bot terminated by other getUpdates request make sure that only one bot instance is running

大家好在这个模块中(telegram_interface.py)我有(send_msg)方法,我需要在另一个模块中调用它,但是每当我尝试导入这个模块时(telegram_interface) 在其他模块中调用 send_msg 方法时,我遇到了从多个实例调用 getUpdates 的错误,是否可以避免这种情况发生!

telegram_interface.py
import configparser
import telepot
import time
from database import Status
import datetime as dt
import place_orders as po
import requests

config = configparser.ConfigParser()
config.read("config1.ini")

bot_token = str(config["TelegramBot1"]["bot_token"])
my_chat_id = str(config["TelegramBot1"]["my_chat_id"])
bot111 = telepot.Bot(bot_token)


def time_now():
    time = dt.datetime.now()
    time = time.strftime("%H:%M:%S   //   %d-%m-%Y")
    return time


# def send_msg(text):
#     url = "https://api.telegram.org/bot"+bot_token + \
#         "/sendMessage?chat_id="+my_chat_id+"&parse_mode=Markdown&text="
#     http_request = url + text
#     response = requests.get(http_request)
#     return response.json()


def handle(msg):
    user_name = msg["from"]["first_name"] + " " + msg["from"]["last_name"]
    content_type, chat_type, chat_id = telepot.glance(msg)

    if content_type == "text":
        command = msg["text"]
        if "/START" == command.upper():
            bot111.sendMessage(chat_id,
                               "Welcome "+user_name+" in your Auto Trading Bot! \n command [/help] to get more information about the bot. ")
        elif "/HELP" == command.upper():
            bot111.sendMessage(chat_id,
                               "Available commands are: \n **[ON, OFF] - Control the bot. \n **[balance] - Get your free USDT balance.")
        elif "ON" == command.upper():
            Status.save_status(collection="Status",
                               status=command.upper(), time=time_now())
            bot111.sendMessage(chat_id, "System is *Activated*.",
                               parse_mode="Markdown")
            with open("log.txt", "a") as log_file:
                log_file.write(
                    "System is Activated at : " + time_now() + "\n")
        elif "OFF" == command.upper():
            Status.save_status(collection="Status",
                               status=command.upper(), time=time_now())
            bot111.sendMessage(chat_id, "System is *Deactivated*.",
                               parse_mode="Markdown")
            with open("log.txt", "a") as log_file:
                log_file.write("System is Deactivated at : " +
                               time_now() + "\n")
        elif "BALANCE" == command.upper():
            free_balance = po.get_usdt_balance()
            bot111.sendMessage(chat_id,
                               "Your free balance is : *"+str(free_balance)+" USDT*", parse_mode="Markdown")
        else:
            bot111.sendMessage(chat_id,
                               "Unknown command, use [/help] to get more information.")


bot111.message_loop(handle)


while True:
    time.sleep(20)

当您在 Python 中导入任何模块时,它正在被执行,例如假设我们有以下模块 print_hello.py:

print('hello world')

如果你运行它,它会打印hello world。如果导入此模块:

import print_hello

它也会打印 hello world!所以,如果你多次导入它,它会打印 hello world 恰好那么多次。 为避免这种情况,您需要模拟主入口点,编辑 print_hello.py:

if __name__ == '__main__':
    print('hello world')

在这种情况下,hello world 将仅使用 运行ning print_hello.py 打印,但如果导入则不会打印。 因此,您应该将其应用于您的代码行:

bot111.message_loop(handle)


while True:
    time.sleep(20)