标签: python-telegram-bot

安装 python-telegram-bot 时出现错误“未找到包‘libffi’”

我尝试在我的树莓派上安装 python-telegram-bot 但遇到了这个问题:

Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-QLuRQr/cffi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-1vjudm-record/install-record.txt --single-version-externally-managed --compile --user --prefix=:
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libffi' found
    Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libffi' found
    Package …
Run Code Online (Sandbox Code Playgroud)

python raspberry-pi python-telegram-bot

4
推荐指数
1
解决办法
1万
查看次数

如何修复导入错误:没有名为“telebot”的模块

我正在配置一个机器人从 Zabbix 发送警报,因此我安装了 Python 和模块:

sudo apt install python python-pip python-setuptools
Run Code Online (Sandbox Code Playgroud)

之后,我安装了 bot API 以在 Zabbix 上使用:

python -m pip install --user pyTelegramBotAPI
Run Code Online (Sandbox Code Playgroud)

在/usr/lib/zabbix/alertscripts/中创建脚本:

#!/usr/bin/env python

import telebot,sys

BOT_TOKEN='123TOKENAQUI321'
DESTINATION=sys.argv[1]
SUBJECT=sys.argv[2]
MESSAGE=sys.argv[3]

MESSAGE = MESSAGE.replace('/n','\n')
tb = telebot.TeleBot(BOT_TOKEN)
tb.send_message(DESTINATION,SUBJECT + '\n' + MESSAGE)
Run Code Online (Sandbox Code Playgroud)

更改的权限:

sudo chmod +x telegram

sudo chown -R zabbix telegram
Run Code Online (Sandbox Code Playgroud)

在终端或 Zabbix 上测试脚本时,出现以下错误:

回溯(最近一次调用最后一次):文件“/usr/lib/zabbix/alertscripts/telegram”,第 2 行,导入 telebot,sys ImportError: 没有名为“telebot”的模块

我尝试通过安装模块来解决:

python -m pip install --user telebot
Run Code Online (Sandbox Code Playgroud)

安装模块没有解决它,所以我尝试使用python3,终端上的脚本工作了,但在Zabbix中仍然显示相同的错误。我最终又回到了Python。

telebot 模块不会出现pip list,仅在 python 终端内使用该命令出现help ("modules") …

python zabbix telegram python-telegram-bot telegram-bot

4
推荐指数
1
解决办法
3万
查看次数

Telegram 机器人可以检测到加入频道活动的新成员吗?

我有一个 Telegram 机器人,我将此机器人设置为频道管理员。现在,当新用户通常加入频道(而不是群组)时,频道上没有消息,我们也无法通过getUpdates方法获取消息。从技术上讲,是否可以发布一条消息说 \xe2\x80\x9cA 新用户已加入频道 \xe2\x80\x9d - 这会捕获 \xe2\x80\x9cnew member\xe2\x80\x9d 事件?让我知道。

\n

telegram python-telegram-bot telegram-bot telegram-webhook node-telegram-bot-api

4
推荐指数
1
解决办法
2万
查看次数

深度链接到电报机器人

我有一个简单的用例。当用户点击下面的链接时,

T.me/MycompanynameBot?start=Microsoft

我想向他展示 3 个内联按钮,分别对应 Microsoft 内的 3 个 Telegram 频道。这可能吗?

关键是机器人必须能够从 URL 检索参数。

谢谢。

telegram python-telegram-bot telegram-bot telegram-webhook node-telegram-bot-api

4
推荐指数
1
解决办法
5773
查看次数

如何在电报机器人对话处理程序的对话中保存数据

我正在构建一个电报机器人。

我的机器人的功能之一要求机器人询问用户以在选项之间进行选择。听到的是我查询用户的代码

def entry_point(update: Update, context: CallbackContext):
    companies_btn = [
        [
            InlineKeyboardButton(company.company_name, callback_data=company.id)
            for company in get_companies()
        ]
    ]
    companies_keyword = InlineKeyboardMarkup(companies_btn)
    update.message.reply_text(
        "Please pick a company", reply_markup=companies_keyword
    )
    return 1


def user_picked_company(update: Update, context: CallbackContext):
    stores_btn = [
        [
            InlineKeyboardButton(store.store_name, callback_data=store.id)
            for store in get_stores()
        ]
    ]

    store_keyword = InlineKeyboardMarkup(stores_btn)
    update.message.reply_text(
        "Please pick a store", reply_markup=store_keyword
    )
    return 2


def user_picked_store(update: Update, context: CallbackContext):
    save_user_choices()


handler = ConversationHandler(
    entry_points=[CommandHandler('pick', entry_point)],
    states={
        1: CallbackQueryHandler(user_picked_company),
        2: CallbackQueryHandler(user_picked_store)
    },
    fallbacks=[entry_point],
    per_chat=True,
    per_user=True, …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-telegram-bot telegram-bot

4
推荐指数
1
解决办法
7832
查看次数

通过 python 向 Telegram Bot 发送短信

我从早上开始就一直在尝试,但早些时候出现了错误,所以我有了方向,但现在没有错误,甚至没有警告。

代码如下:

import requests

def send_msg(text):
token = "TOKEN"
chat_id = "CHATID"
url_req = "https://api.telegram.org/bot" + token + "/sendMessage" + "?chat_id=" + chat_id + "&text=" + text 
results = requests.get(url_req)
print(results.json())

send_msg("hi there 1234")
Run Code Online (Sandbox Code Playgroud)

预期输出是什么:它应该发送一条短信

当前输出是什么:什么也不打印

如果有人帮忙,那就太好了,谢谢大家

编辑:2

由于未安装以下依赖项,因此无法发送文本。

$ pip install flask
$ pip install python-telegram-bot
$ pip install requests
Run Code Online (Sandbox Code Playgroud)

现在有人可以帮我发送照片吗?我认为它无法通过 URL 发送图像,谢谢大家

**编辑3 **

我从这里找到了一个图像或视频共享网址,但我的图像是本地图像,而不是来自远程服务器

python telegram python-telegram-bot

4
推荐指数
1
解决办法
2万
查看次数

使用 Telethon 自动登录 Telegram 客户端 (python)

我正在尝试使用 Telethon 库编写一个访问 Telegram 客户端的电报机器人。下面的代码中一切正常,但运行代码时,Telegram Auth 过程是通过终端运行的。有没有办法自动化该过程,以便我可以使用 Python 登录客户端(无需在终端中输入)。

Auth 过程要求:

  • 电话号码
  • 密码
  • 安全码

我想要实现的是,当用户调用某个命令时,机器人会启动客户端登录过程,并要求用户输入密码和安全代码,然后用于登录客户端。该机器人将使用 python-telegram-bot 库来管理与用户的对话,同时它将使用 Telethon 库来连接到客户端。这可能吗?谢谢

这是主文件:(一个工作测试示例,尝试在使用 python-telegram-bot 时登录 Telethon Telegram 客户端)

from telethon import TelegramClient
from karim.secrets import secrets
import asyncio

# this def gets called when the /telethon command is sent by the user to the bot
def telethonMessage(update, context):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    api_id = secrets.get_var('API_ID')
    api_hash = secrets.get_var('API_HASH')
    client = TelegramClient('anon', api_id, api_hash, loop=loop)
    with client:
        loop.run_until_complete(send_telethon_message(client, update.effective_user.id))
     

async def send_telethon_message(client, user_id):
    me …
Run Code Online (Sandbox Code Playgroud)

python telegram python-telegram-bot telegram-bot telethon

4
推荐指数
1
解决办法
2万
查看次数

如何使用Python在Telegram机器人中发送表情符号?

我正在处理一个小项目,我决定添加表情符号以使视觉效果更好一点,但我无法发送它。我尝试过像“\U000203C”这样的 Unicode,甚至尝试复制表情符号并粘贴它,但仍然无法做到。有什么方法可以发送表情符号吗?

统一码

exchange_msg = "TRY TO USD: "+ USDTORTY, "PERCANTAGE: " +  USDTORTYPERCENTAGE + u'\U000203C'
update.message.reply_text(exchange_msg)
Run Code Online (Sandbox Code Playgroud)

复制粘贴

exchange_msg = "TRY TO USD: "+ USDTORTY, "PERCANTAGE: " +  USDTORTYPERCENTAGE + u''
update.message.reply_text(exchange_msg)
Run Code Online (Sandbox Code Playgroud)

我尝试的代码的输出如下。

["TRY TO USD: 7.8645", "PERCANTAGE: -0.0151 (-0.19%)\ud83d\udea8"]
Run Code Online (Sandbox Code Playgroud)

python-3.x python-telegram-bot telegram-bot

4
推荐指数
1
解决办法
2万
查看次数

尝试发送长降价消息时出现“telegram.error.BadRequest:Entities_too_long”错误。最大字符串长度限制是多少?

这是当我尝试发送包含大量格式化超链接的长降价消息时出现的错误:

context.bot.send_message(chat_id=update.effective_chat.id, text=longmsg, parse_mode="MARKDOWN")

#Output:
telegram.error.BadRequest: Entities_too_long
Run Code Online (Sandbox Code Playgroud)

实际消息的长度几乎没有 500 个字符,但几乎每个单词都带有超链接,这使得字符串longmsg非常长(>12k 字符串长度)

现在,我知道对于普通消息,4096 个字符是限制,但在发送 Markdown 消息时,Telegram API 似乎允许 > 4096 个字符串长度。那么 Markdown 消息的确切字符串长度限制是多少?我正在使用python-telegram-bot图书馆。

telegram python-telegram-bot telegram-bot

4
推荐指数
1
解决办法
6650
查看次数

AttributeError:“更新程序”对象没有属性“调度程序”

当我运行这段代码时:

from telegram.ext import *
import keys
    
print('Starting a bot....')
     
def start_commmand(update, context):
    update.message.reply_text('Hello! Welcome To Store!')

if __name__ == '__main__':
    updater = Updater(keys.token, True)
    dp = updater.dispatcher

    # Commands
    dp.add.handler(CommandHandler('start', start_commmand))

    # Run bot
    updater.start_polling(1.0)
    updater.idle()
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Traceback (most recent call last):
  File "C:\Users\pc\PycharmProjects\telegram\main.py", line 11, in <module>
    dp = updater.dispatcher
AttributeError: 'Updater' object has no attribute 'dispatcher'
Run Code Online (Sandbox Code Playgroud)

我尝试通过更新库来解决此问题,但错误仍然存​​在。

python python-telegram-bot

4
推荐指数
1
解决办法
1万
查看次数