When sending a Telegram message other than text, the Telegram Agent downloads the file to a temporary file using parts of the URL. URLs can be quite long, longer than the maximum filesystem path length thus causing an exception to be thrown.
However, downloading files to the file system is unnecessary because the Telegram Bot API accepts HTTP URLs passed as in the corresponding file fields: photo, document, audio, video, etc. See https://core.telegram.org/bots/api#sending-files
The Telegram Bot API also accepts references to files stored on their platform called file_ids, therefore attempting to download them to a temporary file will also fail. These types of references should just be passed as-is.
Example of exception thrown:
Exception during receive. File name too long @ rb_sysopen - /tmp/thumbnail?scale=1&hl=en-CA&h=281&ll=51.1581%2C-56.03265&cacheBuster=f2e6393f721a81b5&w=500&spn=1.8828%2C1.9147&lyrs=m%2Cpublicalerts.met%2Cpublicalerts.asid.60afa9bc9d60eded%7Cpublic_caching%3A1200&z=720180505-287-x0zyoy.60afa9bc9d60eded%7Cpublic_caching%3A1200&z=7: /usr/lib/ruby/2.5.0
/tempfile.rb:133:in `initialize'
/usr/lib/ruby/2.5.0/tempfile.rb:133:in `open'
/usr/lib/ruby/2.5.0/tempfile.rb:133:in `block in initialize'
/usr/lib/ruby/2.5.0/tmpdir.rb:128:in `create'
/usr/lib/ruby/2.5.0/tempfile.rb:131:in `initialize'
/app/app/models/agents/telegram_agent.rb:121:in `new'
/app/app/models/agents/telegram_agent.rb:121:in `load_file'
/app/app/models/agents/telegram_agent.rb:117:in `load_field'
/app/app/models/agents/telegram_agent.rb:131:in `block (2 levels) in receive_event'
/app/app/models/agents/telegram_agent.rb:130:in `each'
/app/app/models/agents/telegram_agent.rb:130:in `count'
/app/app/models/agents/telegram_agent.rb:130:in `block in receive_event'
/app/app/concerns/liquid_interpolatable.rb:64:in `interpolate_with'
/app/app/models/agents/telegram_agent.rb:129:in `receive_event'
/app/app/models/agents/telegram_agent.rb:86:in `block in receive'
However, downloading files to the file system is unnecessary because the Telegram Bot API accepts HTTP URLs passed as in the corresponding file fields: photo, document, audio, video, etc. See https://core.telegram.org/bots/api#sending-files
Nice, that really sounds like we can remove that part of the Agent and just pass the URL to Telegram directly without breaking anybody's configuration.
The Telegram Bot API also accepts references to files stored on their platform called file_ids, therefore attempting to download them to a temporary file will also fail. These types of references should just be passed as-is.
How are you getting those file_ids, via a different Telegram API? If I understand the API documentation correctly this should work if the just pass the URL/string the Agent receives in the text/photo/etc to Telegram without first downloading the file.
Nice, that really sounds like we can remove that part of the Agent and just pass the URL to Telegram directly without breaking anybody's configuration.
Yup, indeed.
How are you getting those
file_ids, via a different Telegram API? If I understand the API documentation correctly this should work if the just pass the URL/string the Agent receives in thetext/photo/etcto Telegram without first downloading the file.
Yes, that's right.
There's a few ways I know on how to get file_ids:
getUpdates API method, where each Update in the response containing media will have such file_ids in their media key (photo,video) within the effective message (message, channel_post, edited_message, ...). For example: update.channel_post.photo[-1].file_idsendPhoto, sendVideo, sendDocument, etc., the API response will contain such information in JSON. For photos, the file_id is in photo[-1].file_id. For other kinds of media, the file_id is in video.file_id (replace video with document,gif,sticker,document, etc.)An enhancement for the Telegram Agent would be that it generates events with the response from the API, but I suggest we leave that for another issue.
Sounds good to me, are you interested in working on a PR for the changes?
I don't know Ruby, but I can give it a try.
Please check my PR https://github.com/huginn/huginn/pull/2285
PR was merged. Closing.