python-telegram-bot을 쓰다가 다른 API를 써보려고 검색하다보니 telegram 사이트에 언어별로 라이브러리를 정리해둔 웹페이지를 발견했다.
https://core.telegram.org/bots/samples
Bot API Library Examples
A full tutorial covering everything from configuring your environment to deploying your finished bot is available here.…
core.telegram.org
아래의 라이브러리들이 Python을 위한 대표적인 라이브러리들로 소개하고 있는데 일단 두번째인 pyTelegramBotAPI가 괜찮을 것 같아서 사용해보기로 했다.

https://github.com/eternnoir/pyTelegramBotAPI#telebot
GitHub - eternnoir/pyTelegramBotAPI: Python Telegram bot api.
Python Telegram bot api. Contribute to eternnoir/pyTelegramBotAPI development by creating an account on GitHub.
github.com
send_message함수를 그대로 사용한 것과 threading으로 처리하는 것 둘다 이상없이 작동된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import telebot
import threading
from datetime import *
class TelegramClass():
def __init__(self):
self.token = "mytoken#!@#$%^&*()"
self.chat_id = 1234567890
self.bot = telebot.TeleBot(self.token)
# telebot.send_message
def message(self, text):
start = datetime.now()
self.bot.send_message(self.chat_id, text)
print('telebot.send_message', datetime.now() - start, text)
# telebot.send_message threading ver
def thread_message(self, text):
start = datetime.now()
task = threading.Thread(target=self.bot.send_message,
args=(self.chat_id, text,))
task.start()
print('telebot.send_message threading ver', datetime.now() - start, text)
main = TelegramClass()
text = 'pyTelegramBotAPI Test Message'
main.thread_message(text)
main.message(text)
|
cs |

'Python, API' 카테고리의 다른 글
[Python] KRX 주가데이터로 수정주가 계산 (0) | 2023.03.25 |
---|---|
Pandas.read_sql 속도 비교 (chunksize, connectorx) (0) | 2023.02.26 |
[Python-telegram-bot] bot.sendMessage - RuntimeWarning: coroutine 'Bot.send_mes (0) | 2023.02.04 |
Python 3.11의 속도개선 (feat. 5950X vs 7950X) (0) | 2023.01.07 |
투자 관련 API를 사용할 때 조심해야 할 점 (0) | 2022.11.28 |